javascript JQuery 验证插件 submitHandler 对我不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18354238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Validation Plugin submitHandler doesnt work for me
提问by Grandy
Hello i got the following issue my following code doesn't work for me and i cant figure out why. I hope you guys can help me.
您好,我遇到了以下问题,我的以下代码对我不起作用,我不知道为什么。我希望你们能帮助我。
Edit: The submit handler simply doesn't get called. I don't even get the alert message. I wanted to disable the submit button till the form is validated.
编辑:提交处理程序根本不会被调用。我什至没有收到警报消息。我想禁用提交按钮,直到表单被验证。
$('form').validate({
onsubmit: true,
debug: true,
errorLabelContainer: "#errorBox",
rules: {
user_email: {
minlength: 4,
email: true,
required: true
},
user_textField: {
minlength: 3,
required: true
},
user_phone: {
required: true,
number: true,
minlength: 4
},
user_fax: {
number: true,
minlength: 4
},
user_plz: {
required: true,
number: true,
minlength: 5
}
},
submitHandler: function (form) {
$('form').find(":submit").attr("disabled", true).attr("value","Submitting...");
alert("hie");
form.submit();
}
});
I also got a submit button:
我还有一个提交按钮:
<button class="btn btn-primary pull-right continuePaymentProcess" type="submit">Weiter</button>
Best Regards Grandy
最好的问候爷爷
回答by Sparky
You don't need the onsubmit: true
line because that's the default behavior. Important: as per docs,
您不需要该onsubmit: true
行,因为这是默认行为。 重要: 根据文档,
Validate the form on submit. Set to false to use only other events for validation. Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value.
在提交时验证表单。设置为 false 以仅使用其他事件进行验证。设置为 Function 以自己决定何时运行验证。布尔值 true 不是有效值。
In other words, you must remove that line because true
will break the plugin.
换句话说,您必须删除该行,因为true
会破坏插件。
As far as the submitHandler
"not working", it works as per the documentation. The submitHandler
callback function fires when the form is valid.
至于submitHandler
“不工作”,它按照文档工作。该submitHandler
回调函数触发时的形式是有效的。
You say, "I wanted to disable the submit button till the form is validated."
你说,“我想在表单被验证之前禁用提交按钮。”
Since the submitHandler
doesn't get fired until the the form is valid
, your code to disable the button doesn't fire until afterthe form is validated.
由于在submitHandler
表单为 之前不会触发valid
,因此您禁用按钮的代码在表单验证后才会触发。
See your code: http://jsfiddle.net/aDAGL/
查看您的代码:http: //jsfiddle.net/aDAGL/
回答by Upendra Manve
I too faced the same issue. Please check all the input elements in the form and provide "name" attribute to all the elements . This helped in my case.
我也面临同样的问题。请检查表单中的所有输入元素,并为所有元素提供“名称”属性。这对我有帮助。
Thank you
谢谢