jQuery Validate-对隐藏字段启用验证 [英] jQuery Validate - Enable validation for hidden fields

查看:86
本文介绍了jQuery Validate-对隐藏字段启用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,在新版本的jQuery验证插件1.9中隐藏字段的验证被忽略 .我将CKEditor用于textarea输入字段,它隐藏了该字段并将其替换为iframe.该字段在那里,但是对隐藏字段禁用验证.有了验证插件1.8.1版,一切都会按预期进行.

In the new version of jQuery validation plugin 1.9 by default validation of hidden fields ignored. I'm using CKEditor for textarea input field and it hides the field and replace it with iframe. The field is there, but validation disabled for hidden fields. With validation plugin version 1.8.1 everything works as expected.

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

So my question is how to enable validation for hidden fields with v1.9 validation plugin.

此设置无效:

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

推荐答案

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

The plugin's author says you should use "square brackets without the quotes", []

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

发布:验证插件1.9.0: "...另一项更改应使表单具有隐藏元素 更容易,现在默认情况下将其忽略(选项"ignore"具有 现在默认为:hidden").从理论上讲,这可能会破坏现有的 设置.在不太可能的情况下,您可以通过以下方法修复它: 将忽略选项设置为"[]"(不带方括号的方括号 引号).

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

要为所有表单更改此设置,请执行以下操作:

To change this setting for all forms:

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

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

(It is not required that .setDefaults() be within the document.ready function)

或为一种特定形式:

$(document).ready(function() {

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

});


编辑:

有关如何在某些隐藏字段上启用验证但仍然忽略其他字段的信息,请参见此答案.

See this answer for how to enable validation on some hidden fields but still ignore others.

编辑2 :

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

Before leaving comments that "this does not work", keep in mind that the OP is simply asking about the jQuery Validate plugin and his question has nothing to do with how ASP.NET, MVC, or any other Microsoft framework can alter this plugin's normal expected behavior. If you're using a Microsoft framework, the default functioning of the jQuery Validate plugin is over-written by Microsoft's unobtrusive-validation plugin.

如果您在使用unobtrusive-validation插件苦苦挣扎,请改用以下答案: https://stackoverflow.com /a/11053251/594235

If you're struggling with the unobtrusive-validation plugin, then please refer to this answer instead: https://stackoverflow.com/a/11053251/594235

这篇关于jQuery Validate-对隐藏字段启用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆