简体   繁体   中英

jQuery xPath selector, &&

I'm using jQuery to run through all the inputs within a section that I have set from hidden to show, and add required calsses, so as to catch the validation.

$("#" + dependant).find("input[type!='radio']").addClass('required');

Works fine, but I also want to exclude the submits. In normal xPath I would;

input[type!='radio' and type!='submit']

I acheived a work around using .each() and an additional IF;

$("#" + dependant).find("input[type!='radio']").each(function()
{
    if ($(this).attr(type) != 'submit')
    {
        $(this).addClass('required')
    }
});

But it strikes me there must be an easier, cleaner way of combining contraints....

See Traversing and Selectors in the jQuery docs.

$("#" + dependant).find(":input").not(':radio').not(':submit').addClass('required');

You can do:

$("#" + dependant).find("input[type!='radio'][type!='submit']").addClass('required');

See multiple attribute selector :

Description : Matches elements that match all of the specified attribute filters.

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