简体   繁体   English

jQuery.validator无法将默认忽略选择器分配给jQuery.validator.defaults.ignore

[英]jQuery.validator cannot assign default ignore selector to jQuery.validator.defaults.ignore

I know from here that I can pass an ignore option in the jQuery.validate method like this... 我从这里知道我可以在jQuery.validate方法中像这样传递忽略选项...

$('form').validate({
    ignore: ":hidden"
});

... which works fine, but I wanted to set the validator to ignore ":hidden" form fields by default... I've tried the following: ...效果很好,但是我想默认设置验证器忽略“:hidden”表单域...我尝试了以下操作:

jQuery.validator.defaults.ignore.push(":hidden");

... but it doesn't work. ...但是它不起作用。

To hide the form fields I've wrapped them in a div and I hide the div; 为了隐藏表单域,我将它们包装在div中,然后隐藏了div。 which by default has "display: none" applied. 默认情况下已应用“显示:无”。

Anyone have any ideas? 有人有想法么?

Edit: I've been wondering after a bit of a look in firebug, whether the "not()" method in jQuery is not meant to be able to take an array... which is what the jQuery.validator.defaults.ignore property is, or is it something else? 编辑:我一直想知道在萤火虫中看了一下之后,jQuery中的“ not()”方法是否不意味着能够采用数组...这就是jQuery.validator.defaults.ignore财产是,还是其他?

Edit 2 I've noticed that if I do this: 编辑2我注意到,如果我这样做:

jQuery.validator.defaults.ignore = ":hidden";

it works... again, i think it might be that it is an array... 它有效...再次,我认为这可能是一个数组...

Results It seems that using the code in Edit 2 above is not unreasonable as when you assign 'ignore: ":hidden"' in the validate() method you are essentially doing the same things... What got me was that the item in the defaults is an array. 结果似乎使用上面的Edit 2中的代码并不是没有道理的,因为当您在validate()方法中分配'ignore:“:hidden”'时,您实际上是在做同样的事情……令我明白的是,默认值为数组。

One of the overloads for .not() is .not([elements]) , see here for details . .not()的重载之一是.not([elements])有关详细信息请参见此处 The initial declaration of [] is passing an empty set of elements, leaving nothing for jQuery to remove. []的初始声明传递了一组空元素,没有任何内容可供jQuery删除。 When you change the type to a string, you're calling a different overload of .not() 当您将类型更改为字符串时,您正在调用不同的.not()重载。

Though the defaults.ignore property is an array it is passed to the jQuery not() method, which takes a selector... so by assigning a string as follows, the validator ignores elements out of view. 尽管defaults.ignore属性是一个数组,但是它传递给jQuery not()方法,该方法带有一个选择器...因此,通过按以下方式分配字符串,验证程序会忽略视图之外的元素。 This works: 这有效:

jQuery.validator.defaults.ignore = ":hidden"; 

The only real question i would have is why it is an array in the first place. 我唯一真正的问题是,为什么它首先是数组。 It's defined like this: 它的定义如下:

ignore: []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM