问题描述
我目前正在设置jQuery验证插件,以便在我们的项目中使用。
I am currently setting up the jQuery validation plug-in for use in our project.
默认情况下,会自动设置一些事件进行处理。即聚焦进/出,关键事件对所有输入进行火灾验证。我希望它只在单击提交按钮时触发。
By default, there are some events automatically set up for handling. I.e. focus in/out, key up events on all inputs fire validation. I want it to only fire when the submit button is clicked.
此功能似乎内置于插件中,这使得执行此操作变得困难(没有修改插件代码,不是我想要做的事情)。
This functionality seems to be in-built into the plug-in, which is making it difficult to do this (without modifying the plug-in code, Not What I Want To Do).
我在插件代码原型方法中找到了eventDelegate函数调用:
I have found the eventDelegate function calls in the plugin code prototype method:
$(this.currentForm)
.validateDelegate(":text, :password, :file, select, textarea", "focusin focusout keyup", delegate)
.validateDelegate(":radio, :checkbox, select, option", "click", delegate);
当我从插件中删除这些行时,我得到了我的结果,但我宁愿做插件之外的东西来实现这一目标。
When I remove these lines from the plug-in I get my result, however I would much rather do something Outside the plug-in to achieve this.
有人可以帮帮我吗?如果您需要更多详细信息,请告诉我们。我搜索谷歌的效果不大。
Can anybody please help me? If you need any more details, please let me know. I have searched google with little success.
谢谢
编辑:
我基本上只是在提交事件被触发时才尝试验证表单。默认情况下,每次输入控件中的焦点丢失时,插件都会进行验证。
EDIT:I am basically trying to validate the form Only when the submit event is fired. By default, the plug-in validates every time focus is lost in an input control.
推荐答案
找到答案。它是(隐藏?)作为验证方法中选项的一部分。
Found the answer. It was (hidden?) away as part of the options in the validate method.
在此页面上查看onfocusout等选项:
See the options onfocusout etc on this page:http://docs.jquery.com/Plugins/Validation/validate#options
我可以设置为false。
which I can set to false.
这是我设置验证器的代码(希望其他人会觉得这很有用):
Here is my code which sets up my validator (hopefully others will find this useful):
$(document).ready(function() {
$("form").each(function() {
$(this).validate({
validateDelegate: function() { },
onsubmit: true,
onkeydown: false,
onkeyup: false,
onfocusin: false,
onfocusout: false,
errorContainer: "#PanelError",
errorLabelContainer: "#PanelError ul",
wrapper: "li",
ignoreTitle: true,
errorClass: "Error",
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
$(element.form).find("#" + element.id)
.addClass(errorClass);
},
unhighlight: function(element, errorClass, validClass) {
$(element).removeClass(errorClass).addClass(validClass);
$(element.form).find("#" + element.id)
.removeClass(errorClass);
}
});
});
});
这篇关于在jQuery验证插件中自定义事件委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!