简体   繁体   English

如何使用jQuery选择具有类并以属性结尾的元素?

[英]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; 我有一组是/否单选按钮,我想使用jQuery选择; however, I only want to select ones with the class "required" and ones that end with the id "yes". 但是,我只想选择类为“ required”的类和以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". 我只想选择类别为“ required”的那些和以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”的元素也具有以“ yes”结尾的id,则只需将选择器组合在同一字符串中:

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

(Note: don't leave a space or it will look for [id$="yes"] elements that are descendants of .required elements.) (注意:请不要留空格,否则它将寻找[id$="yes"]元素是.required元素的后代。)

Or use the .filter() method , which in my opinion is more readable: 或使用.filter()方法 ,我认为该方法更具可读性:

$('.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: 如果您的意思是除了ID以“ yes”结尾的(其他)元素之外,还希望元素具有“ required”类的元素,则将选择器与逗号结合:

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

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

相关问题 Jquery - 如何使用类和属性查找元素 - Jquery - How to find an element using class and attribute JQuery 选择器:使用“包含:”选择具有特定类和“href”属性的元素 - JQuery Selectors: Select element with specific class and 'href' attribute using "contains:" 如何使用'this'在jQuery类中选择特定元素 - How to select particular element with class in jQuery using 'this' 如何 select 元素具有一定的 class 并具有以 class 结尾? - How to select element that has a certain class and has a class that ends with? 使用jQuery,如何选择元素id以某些字符串结尾的所有元素? - Using jQuery, how to select all elements where element id ends with some string? jQuery - 如何使用自定义属性值选择tablerow元素? - jQuery - How to select tablerow element using custom attribute value? 如何使用jQuery将元素标题添加到类属性 - How to add title of element to class attribute using jquery 如何使用Jquery在输入元素中查找class属性是否存在 - How to find whether the class attribute is present or not in the input element using Jquery 如果有类'on'或不使用jQuery,如何向元素添加特殊属性? - How to add special attribute to element if has class 'on' or not using jquery? 使用jQuery通过数据属性选择克隆的元素 - Select cloned element by data attribute using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM