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

bcm*_*cfc 16 jquery jquery-validate

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

这就是我目前所拥有的:

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
}
Run Code Online (Sandbox Code Playgroud)

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

$("input, select, textarea").rules('remove','required');
Run Code Online (Sandbox Code Playgroud)

Chr*_*ens 21

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

$('input, select, textarea').each(function() {
    $(this).rules('remove', 'required');
});
Run Code Online (Sandbox Code Playgroud)

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


bcm*_*cfc 5

有时可能是这种情况,我似乎在诉诸于此处发帖后偶然发现了答案。

for (var i in settings.rules){
   delete settings.rules[i].required;
}
Run Code Online (Sandbox Code Playgroud)

作品。