JQuery Validation插件submitHandler对我不起作用(JQuery Validation Plugin submitHandler doesnt work for me)
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
解决方案
You don't need the onsubmit: true line because that's the default behavior. Important: as per docs,
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.
In other words, you must remove that line because true will break the plugin.
As far as the submitHandler "not working", it works as per the documentation. The submitHandler callback function fires when the form is valid.
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 after the form is validated.
See your code: http://jsfiddle.net/aDAGL/
您好我得到以下问题我的以下代码对我不起作用,我无法弄清楚原因。我希望你们能帮助我。
编辑:提交处理程序根本不会被调用。我甚至没有得到警报信息。我想禁用提交按钮,直到表单被验证。
$('form')。validate({
onsubmit:true,
debug:true,
errorLabelContainer:#errorBox,
规则:{
user_email:{
minlength:4,
email:true,
required :true
},
user_textField:{
minlength:3,
required:true
},
user_phone:{
required:true ,
数字:真,
最小长度:4
},
user_fax:{
数字:真,
最小长度:4
},
user_plz:{
required:true,
number:true,
minlength:5
}
},
submitHandler :function(form){
$('form')。fin d(:submit)。attr(disabled,true).attr(value,Submitting ...);
alert(hie);
form.submit();
}
});
我还有一个提交按钮:
< button class =btn btn-primary pull-right continuePaymentProcesstype =submit> Weiter< / button>
最好的问候Grandy
解决方案
您不需要 onsubmit:true 行,因为这是默认行为。 重要:根据文档,
在提交时验证表单。设置为false以仅使用其他事件进行验证。设置为函数以自行决定何时运行验证。 布尔值true不是有效值。
换句话说,您必须删除该行,因为 true 会破坏插件。
至于 submitHandler 不工作,它按照文档工作。表格有效时, submitHandler 回调函数会触发 。
你说,我想禁用提交按钮,直到表单被验证。
由于 submitHandler 在表单有效之前不会被触发,在 表单验证之后,禁用该按钮的代码才会触发。
查看您的代码: http://jsfiddle.net/ aDAGL /
