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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3317870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:13:53  来源:igfitidea点击:

How do I remove the 'required' rules from all form inputs in jQuery Validate?

jqueryjquery-validate

提问by bcmcfc

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');

采纳答案by bcmcfc

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.

作品。

回答by Christophe Deliens

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".

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

回答by Junior Laucel

When you need to remove a required input, you need also delete the attr of your input, test this.

当您需要删除必需的输入时,还需要删除输入的属性,测试一下。

$('element').rules('remove', 'required'); $('element').removeAttr('required');

$('element').rules('remove', 'required'); $('element').removeAttr('required');

回答by jonmeyer

You can disable jquery validate rules globally for client side validation using the ignore property (see validator options https://jqueryvalidation.org/validate/). This is useful for testing server side validation.

您可以使用 ignore 属性全局禁用客户端验证的 jquery 验证规则(请参阅验证器选项https://jqueryvalidation.org/validate/)。这对于测试服务器端验证很有用。

You can disable all rules

您可以禁用所有规则

$.validator.setDefaults({
  ignore: ":hidden, input, select, textarea"
})

This is a jquery selector so you also do,

这是一个 jquery 选择器,所以你也这样做,

$.validator.setDefaults({
  ignore: ":hidden, input[required], select[required], textarea[required]"
})

For additional inputs just add as needed.

对于其他输入,只需根据需要添加。

You can also remove html validation with something like this for dev environment (since its a slow selector):

您还可以使用类似这样的东西为开发环境删除 html 验证(因为它是一个缓慢的选择器):

$("*[required]").removeAttr("required");
$("*[maxlength]").removeAttr("maxlength");
$("*[minlength]").removeAttr("minlength");
$("*[pattern]").removeAttr("pattern");