html – 使用window.location.href重定向表单的提交
发布时间:2020-12-14 19:45:24 所属栏目:资源 来源:网络整理
导读:尝试这个简单的代码,我很惊讶它没有用,我们走了: form method="get" action="" input type="submit" value="validate" onclick="redirect(); alertIt(); "//formscript function redirect(){ window.location.href = "test.html" ; }/scriptscriptfunction
尝试这个简单的代码,我很惊讶它没有用,我们走了:
<form method="get" action="" > <input type="submit" value="validate" onclick="redirect(); alertIt(); "/> </form> <script> function redirect(){ window.location.href = "test.html" ; } </script> <script> function alertIt(){ alert("redirect"); } </script> 代码只是在点击提交按钮时重定向到“test.html”,但它没有做到.另一方面:alertIt()工作正常……我的问题如下:事件处理表单中是否有一些我应该知道的特殊规则? 解决方法
如果您希望表单不提交,则需要返回false.
function redirect(){ window.location.href = "test.html" ; } 应该 function redirect(){ window.location.href = "test.html" ; return false; } 您还应该优先挂钩提交事件而不是click事件,并在处理程序中返回. onclick="return redirect(); alertIt(); " 会工作(但不警惕) 最理想的是,您要做的是在表单中添加一个id,例如myForm,并为其附加一个事件. document.getElementById("myForm").addEventListener("submit",function(){ alertIt(); return redirect(); },false); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |