jQuery 按属性值过滤

2024-01-27

<div class="selectedColumns" >
<a href="#" attributeid="19" >Driver License State</a>
<a href="#" attributeid="21" >Email</a>
<a href="#" attributeid="23" >Experience Level</a>
<a href="#" attributeid="26" >First Name</a>
<a href="#" attributeid="71" >Is Account Enabled</a>
<a href="#" attributeid="39" >Last Contacted Date</a>
<a href="#" attributeid="40" >Last Name</a>
<a href="#" attributeid="41" >Middle Name</a>
<a href="#" attributeid="6">Carrier</a>
</div>

我有一个链接集合。每个链接都有一个 attributeid 属性。我想按属性值进行过滤。因此,在上面的链接中,如果我的值为 41,它将返回中间名链接。

var link = $('.selectedColumns a:[attributeid==' + $(this).val() + ']');

这不起作用?


并不是说这更优雅,但在集合上使用 filter() 可以让您在匹配内容上更加灵活,并且比字符串连接更不容易出错。

var matching = $('.selectedColumns a').filter(function(){
                   return $(this).attr('attributeid') == 41
                });
    matching.prop('selected', true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 按属性值过滤 的相关文章