仅对几个特定的​​隐藏字段启用验证

Enabling validation for only a few specific hidden fields

作为 this question 的推论,我将如何配置 jQuery 验证以验证表单中的 一些 隐藏字段,但不是全部?

jQuery 验证 ignores hidden fields by default,这是 ASP.NET 项目中的一个问题。例如,一个页面可能有一个表单,其中包含复选框助手(生成隐藏 inputs),以及可能使用许多隐藏输入的自定义下拉列表。

$(document).ready(function() {
    $('#myform').validate({
        ignore: [],
        // any other options and/or rules
    });
});

像这样使用配置对象,有什么方法可以设置它以允许我选择要验证的特定隐藏输入,无需指定插件必须忽略每个单独的输入?

或者,是否有我可以在这种情况下利用的验证事件挂钩?

default value for the ignore property is ":hidden". You could simply use the :not() pseudo class 使特定 class 不被忽略。

例如,如果您使用选择器 ":hidden:not(.do-not-ignore)",jQuery 验证将忽略所有隐藏元素,但带有 class .do-not-ignore:[=18 的隐藏元素除外=]

$('#myform').validate({
    ignore: ':hidden:not(.do-not-ignore)',
    // any other options and/or rules
});

在此过程中,您可以将 class .do-not-ignore 添加到您仍要验证的隐藏元素中。