简体   繁体   中英

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

$('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:

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; 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?

Edit 2 I've noticed that if I do this:

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.

One of the overloads for .not() is .not([elements]) , see here for details . The initial declaration of [] is passing an empty set of elements, leaving nothing for jQuery to remove. When you change the type to a string, you're calling a different overload of .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. 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: []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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