为一个输入打开/关闭 Jquery 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19016053/
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
Turn on/off Jquery Validation for one input
提问by Joe
I am using the same form to add and edit values. During addition of an item, I want it to force a name check against the server, which it does. However, during edit mode I want it to not validate, since the name may not change. I tried to set the ignore property in the validator to the "ignore" class, based on the documentation, link below. I use that same class name in the name input, but that does not work. It kept validating against the server. Any thoughts?
我使用相同的表单来添加和编辑值。在添加项目期间,我希望它强制对服务器进行名称检查,它确实如此。但是,在编辑模式期间,我希望它不进行验证,因为名称可能不会更改。我试图根据文档,下面的链接,将验证器中的忽略属性设置为“忽略”类。我在名称输入中使用了相同的类名,但这不起作用。它不断对服务器进行验证。有什么想法吗?
URL to the code I am using, primarily the validate function. http://jqueryvalidation.org/validate/
我正在使用的代码的 URL,主要是验证功能。http://jqueryvalidation.org/validate/
Based on the the documentation for the ignore property, it states:
根据 ignore 属性的文档,它指出:
Elements to ignore when validating, simply filtering them out. jQuery's not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements.
验证时要忽略的元素,只需将它们过滤掉即可。使用 jQuery 的 not-method,因此 not() 接受的所有内容都可以作为此选项传递。提交和重置类型的输入总是被忽略,禁用元素也是如此。
I looked in the code and could only see it impacting clean up of errors during the call to validator.resetForm()
. This part of the code works correctly.
我查看了代码,只能看到它在调用validator.resetForm()
. 这部分代码工作正常。
回答by Sparky
You cannot toggle the jQuery Validate plugin on/off for the form or for individual field elements. There is no method or workaround for this.
The
ignore
option is set within the.validate()
call, which is a "one-time" initialization of the plugin, so you cannot change or toggle options dynamically. Any subsequent calls to.validate()
are always ignored.
您不能为表单或单个字段元素打开/关闭 jQuery 验证插件。对此没有任何方法或解决方法。
该
ignore
选项在.validate()
调用中设置,这是插件的“一次性”初始化,因此您不能动态更改或切换选项。任何后续调用.validate()
总是被忽略。
After initializing the plugin with .validate()
, your only solution would be to use the .rules()
method to dynamically 'add'
and 'remove'
rules for individual field elements. Don't be fooled by the 'add'
terminology... this method also allows you to overwrite existing rules, so you don't necessarily have to 'remove'
them first.
使用 初始化插件后.validate()
,您唯一的解决方案是使用该.rules()
方法对各个字段元素进行动态'add'
和'remove'
规则。不要被'add'
术语所迷惑......这种方法还允许您覆盖现有规则,因此您不必先使用'remove'
它们。
To dynamically set the field as required
:
将字段动态设置为required
:
$('#myfield').rules('add', {
required: true // set a new rule
});
To dynamically set the field as notrequired
:
将字段动态设置为notrequired
:
$('#myfield').rules('add', {
required: false // overwrite an existing rule
});
OR
或者
$('#myfield').rules('remove', 'required'); // removes only specified rule(s)
OR
或者
$('#myfield').rules('remove'); // removes all rules for this field
Documentation: http://jqueryvalidation.org/rules/