Skip to main content
 首页 » 编程设计

jquery-validate之jQuery Validate之启用隐藏字段的验证

2024年02月24日49yjmyzz

新版jQuery验证插件1.9中默认validation of hidden fields ignored 。我使用 CKEditor 作为文本区域输入字段,它隐藏该字段并将其替换为 iframe。该字段存在,但隐藏字段的验证已禁用。使用验证插件版本 1.8.1 一切都按预期工作。

所以我的问题是如何使用 v1.9 验证插件启用隐藏字段的验证。

此设置不起作用:

$.validator.setDefaults({ ignore: '' }); 

请您参考如下方法:

该插件的作者说您应该使用“不带引号的方括号”[]

http://bassistance.de/2011/10/07/release-validation-plugin-1-9-0/

Release: Validation Plugin 1.9.0: "...Another change should make the setup of forms with hidden elements easier, these are now ignored by default (option “ignore” has “:hidden” now as default). In theory, this could break an existing setup. In the unlikely case that it actually does, you can fix it by setting the ignore-option to “[]” (square brackets without the quotes)."

要更改所有表单的此设置:

$.validator.setDefaults({  
    ignore: [], 
    // any other default options and/or rules 
}); 

(.setDefaults() 不需要位于 document.ready 函数内)

或者对于一种特定形式:

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

编辑:

参见this answer了解如何对某些隐藏字段启用验证但仍忽略其他字段。

<小时 />

编辑 2:

在留下“这不起作用”的评论之前,请记住OP只是询问the jQuery Validate plugin他的问题与 ASP.NET、MVC 或任何其他 Microsoft 框架如何改变此插件的正常预期行为无关。如果您使用的是 Microsoft 框架,则 jQuery Validate 插件的默认功能将被 Microsoft 的 unobtrusive-validation 插件覆盖。

如果您在使用 unobtrusive-validation 插件时遇到困难,请参阅此答案:https://stackoverflow.com/a/11053251/594235