[英]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.