添加jquery验证插件后,为什么我的表单开始需要双击才能提交?我该如何更改? [英] Why did my form start requiring a double click to submit after I added the jquery validation plug in? How do I change it?
问题描述
我在表单提交按钮上附加了一个commit()事件,在开始使用jquery验证插件之前,我可以单击一下提交此表单.现在由于某种原因,它需要第二次单击.如何使它回到单击状态?
I have a submit() event attached to my form submit button, before I started using the jquery validation plug in I could submit this form on a single click. Now for some reason it takes a second click. How can I make it go back to single click?
$("form").submit(function(){
$(".selector").validate({
rules: {
performance : {
required: true
//customvalidation: true
},
location : {
required: true
//customvalidation: true
},
date : {
required: true
//customvalidation: true
}
},
messages: {
performance : {
required: "enter a show name"
//customvalidation: true
},
location : {
required: "enter a location"
//customvalidation: true
},
date : {
required: "pick a date"
//customvalidation: true
}
}
return false;
}); //closes submit()
});
推荐答案
这是您的全部问题...
This is your whole problem...
$("form").submit(function(){
$(".selector").validate({
...
您不需要将.validate()
放在submit
事件处理程序中.该插件已自动捕获submit
事件. .validate()
方法是表单上插件的 initialization 方法;它仅附加到<c4>元素,然后仅在DOM准备就绪时才调用一次. (您目前需要双击,因为它仅需单击一次即可初始化插件.)
You do not need to put .validate()
inside of a submit
event handler. The plugin already captures the submit
event automatically. The .validate()
method is the initialization method of the plugin on your form; it only gets attached to the form
element and then only called once on DOM ready. (You presently need to double-click because it takes the first click just to initialize the plugin.)
只需执行此操作...
Just do this instead...
$(document).ready(function() {
("#yourform").validate({
// options & rules
});
});
简单的演示: http://jsfiddle.net/x6Ckg/
如果出于某种原因需要在submit
事件上运行其他代码,则可以使用插件提供的submitHandler
回调函数.
If, for whatever reason, you need to run other code on the submit
event, you would use the submitHandler
callback function provided by the plugin.
$(document).ready(function() {
("#yourform").validate({
//options & rules,
submitHandler: function (form) {
// stuff to do when form is valid
// ajax, etc?
}
});
});
这篇关于添加jquery验证插件后,为什么我的表单开始需要双击才能提交?我该如何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!