如何从jQuery Validate中的所有表单输入中删除“必需”规则?

时间:2022-09-24 23:33:45

As above, how can I remove all of the required rules from a form using jQuery Validate?

如上所述,如何使用jQuery Validate从表单中删除所有必需的规则?

This is what I have at present:

这就是我目前所拥有的:

var settings = $('form').validate().settings;

//alert(settings.rules.toSource());
for (var i in settings.rules){
    alert(i);
    delete settings.rules.i; 
    // ^ not sure about how to do this properly,
    // and how to just delete the 'required' rule and keep the rest intact
}

The function native to jQuery Validate returns an undefined error:

jQuery Validate的本机函数返回一个未定义的错误:

$("input, select, textarea").rules('remove','required');

2 个解决方案

#1


18  

And if you don't want to mess with validate's internal settings, you can use the public function in this way:

如果您不想弄乱validate的内部设置,可以这样使用public函数:

$('input, select, textarea').each(function() {
    $(this).rules('remove', 'required');
});

Note that leaving out the second parameter (required) would remove all the rules, not just the "required".

请注意,省略第二个参数(必需)将删除所有规则,而不仅仅是“必需”。

#2


4  

As can sometimes be the case, I appear to have stumbled on the answer just after resorting to posting on here.

有时可能就是这种情况,我似乎在试图在这里发布之后偶然发现了答案。

for (var i in settings.rules){
   delete settings.rules[i].required;
}

Works.

#1


18  

And if you don't want to mess with validate's internal settings, you can use the public function in this way:

如果您不想弄乱validate的内部设置,可以这样使用public函数:

$('input, select, textarea').each(function() {
    $(this).rules('remove', 'required');
});

Note that leaving out the second parameter (required) would remove all the rules, not just the "required".

请注意,省略第二个参数(必需)将删除所有规则,而不仅仅是“必需”。

#2


4  

As can sometimes be the case, I appear to have stumbled on the answer just after resorting to posting on here.

有时可能就是这种情况,我似乎在试图在这里发布之后偶然发现了答案。

for (var i in settings.rules){
   delete settings.rules[i].required;
}

Works.