为什么我的 onsubmit 函数提前退出并且不返回 false?
<form method="post" action="/Order/CheckOut/" onSubmit="return Validate()">
然后...
function Validate() {
alert($("#email").val());
return false;
}
混乱的部分是当我取出警报时它工作正常,提交失败,但是当我放入警报时,它允许提交通过...到底是什么?
我也尝试过这个:
function Validate() {
if(document.getElementByID("email").value == "test"){
alert("It says test.");
}
return false;
}
并得到了相同的行为,它永远不会进入 return 语句...
如果我使用 firebug 执行 JS 并在警报或 if 处中断(取决于上面的版本),它会在那里停止,我单击“步入”,它只是提交表单,为什么它没有进入 return false 行?
<form method="post" action="/Order/CheckOut/" onSubmit="return Validate()">
and then...
function Validate() {
alert($("#email").val());
return false;
}
The messed up part is when I take out the alert it works fine, the submit fails, but when I put the alert in, it allows the submit to go through... what the heck?
I also tried this:
function Validate() {
if(document.getElementByID("email").value == "test"){
alert("It says test.");
}
return false;
}
and got the same behavior, it would never make it to the return statement...
If I step through the JS with firebug and break at the alert or the if (depending on the version above) it stops there, and I click 'step into' and it just submits the form, why isn't it making it to the return false line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不把它包装在 try 块中呢?
Why not wrap it in a try block?
您可以使用
event.preventDefault()
来代替。您应该尝试将 javascript、css 和 html 分开。不要集成它们,否则会使项目更难以管理。不要在 HTML 中使用
onsubmit
属性,只需将您的逻辑从 javascript 附加到表单的$.submit()
方法,就像我上面所做的那样。此示例假设您已将表单命名为“myForm”。我只是在示例中使用了它,因为您应该明确要处理哪个表单的提交事件,而不是使用通用的
$("form")
选择器。You could use
event.preventDefault()
instead.You should attempt to keep your javascript, css, and html all seperate. Don't integrate them, or you'll make the project more difficult to manage. Instead of using the
onsubmit
attribute in HTML, simply append your logic to the$.submit()
method of the form from your javascript as I did above.This example assumes that you've given your form a name of "myForm." I merely used this in the example as you should itendify which form you're handling the submit-event of, and not use a generic
$("form")
selector.如果您已经在使用 jQuery,那么您一开始就做错了。您不应在
If you're already using jQuery you're doing the whole thing wrong to begin with. You shouldn't be manually specifying the
onSubmit
handler from within the<form>
element. Do as @Jon suggested and just bind the submit event:问题不在于你的函数返回 true。问题在于警报中的 JavaScript 发生故障,并且 JavaScript 停止解释代码。此时,浏览器将继续执行默认操作,即提交表单。
这就是为什么 Jonathan Sampson 建议在alert()之前使用e.preventDefault(),这样当alert()失败时浏览器就不会继续其默认行为。
The problem isn't that your function is returning true. The problem is that the JavaScript in the alert is failing and JavaScript quits interpreting the code. At which point, the browser is continuing with the default action which is to submit the form.
This is why Jonathan Sampson suggests using e.preventDefault() before the alert(), this way the browser doesn't continue with its default behaviour when the alert() fails.