简体   繁体   中英

How to select element with class and ends with attribute using jQuery?

I have a set of Yes / No radio buttons that I want to select using jQuery; however, I only want to select ones with the class "required" and ones that end with the id "yes". How do I do this?

$('.required').('[id$="yes"]').on('change',function(){

        ...

});

I only want to select ones with the class "required" and ones that end with the id "yes".

If you mean you want only elements with class "required" that also have an id ending in "yes" then simply combine the selectors within the same string:

$('.required[id$="yes"]')

(Note: don't leave a space or it will look for [id$="yes"] elements that are descendants of .required elements.)

Or use the .filter() method , which in my opinion is more readable:

$('.required').filter('[id$="yes"]')

If you mean that you want elements with class "required" in addition to (other) elements that have an id ending in "yes" then combine the selectors with a comma:

$('.required,[id$="yes"]')

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