jQuery 验证 invalidHandler 与警报仍然在 Firefox 中提交?

发布于 2024-12-03 04:04:06 字数 744 浏览 0 评论 0原文

根本无法理解这一点。 使用验证 jQuery (1.6.2) 和验证插件 (1.8.1)。 如果单击提交按钮,则以下 invalidHandler 代码可以在所有浏览器中正常工作。 但是,如果在 Firefox(无论如何版本 5 和 6)中填写任何字段后通过按键盘上的 Enter 来提交表单,您会收到有关错误数量的警报,但表单仍然会提交! 该表单在其他浏览器中不会提交,如果删除警报线,则在 Firefox 中可以正常工作。但我们需要警报,所以如果有人对如何解决这个问题有任何想法,我们将非常感激 - 我不习惯仅在 Firefox 中发生的事情! :)

invalidHandler: function(form, validator) {
           var errors = validator.numberOfInvalids();
           if (errors)
           {
                   var message = (errors == 1)
                     ? '1 field has an error. It has been highlighted'
                     : errors + ' fields have errors. They have been highlighted';
                     alert(message);
                     //return false;
           }

}

Can't fathom this at all.
Using validate jQuery (1.6.2) with validate plugin (1.8.1).
Have the following invalidHandler code which works fine in all browsers if the submit button is clicked.
However, if the form is submit by hitting enter on the keyboard after filling any field in, in Firefox (versions 5 and 6 anyway), you get the alert about the number of errors but the form still submits anyway!
The form is not submit in other browsers, and if the alert line is removed, it works fine in Firefox. But we need the alert, so if anyone has any ideas on how to fix this it would be greatly appreciated - I'm not used to things acting up just in Firefox! :)

invalidHandler: function(form, validator) {
           var errors = validator.numberOfInvalids();
           if (errors)
           {
                   var message = (errors == 1)
                     ? '1 field has an error. It has been highlighted'
                     : errors + ' fields have errors. They have been highlighted';
                     alert(message);
                     //return false;
           }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2024-12-10 04:04:06

您是否尝试过取消注释 return false

JQuery 文档 表明 false 将:

...通过对事件调用 .preventDefault() 取消提交操作
对象或从我们的处理程序返回 false。

Have you tried uncommenting out return false?

JQuery documentation indicates that false will:

...cancel the submit action by calling .preventDefault() on the event
object or by returning false from our handler.

人疚 2024-12-10 04:04:06

我也出现过这个问题。我认为发生的是对alert() 的调用阻止了invalidHandler 函数的执行,这破坏了jQuery.validate 正常运行的方式。

您可以做的是通过 setTimeout 调用启动警报窗口,这不会阻止 invalidHandler 代码的执行:

setTimeout('alert("'+message+'")', 1)

Hacky,但有效!

I've had this issue occur as well. What I think is going on is that a call to alert() blocks execution of the invalidHandler function, which breaks the way jQuery.validate would normally function.

What you can do is launch an alert window via a setTimeout call, which doesn't block execution of your invalidHandler code:

setTimeout('alert("'+message+'")', 1)

Hacky, but works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文