admin管理员组文章数量:1208153
Given the following XML:
<users>
<user state="CA" sex="m">Max</user>
<user state="AZ" sex="f">Jen</user>
<user state="OR" sex="f">Kim</user>
<user state="NV" sex="m">Bob</user>
<user state="CA" sex="m">Jon</user>
<user state="AZ" sex="m">Jim</user>
<user state="OR" sex="f">Joy</user>
<user state="NV" sex="f">Amy</user>
</users>
Using jQuery, is there a way to select users who are male and are either from CA or NV, but without using the filter function? To be clear, I know that
$(xml).find("user[sex='m']")
selects only male users, while
$(xml).find("user[state='CA'],[state='NV']")
selects all users from either CA or NV. But I am not able to combine both of them with a logical AND within a single selector.
Using the filter function, however, the following works:
$(xml).find("user").filter(function() {
return $(this).attr('sex') == 'm' && ($(this).attr('state') == 'CA' || $(this).attr('state') == 'NV')
}).each(function() {
alert($(this).text());
});
Thanks!
Given the following XML:
<users>
<user state="CA" sex="m">Max</user>
<user state="AZ" sex="f">Jen</user>
<user state="OR" sex="f">Kim</user>
<user state="NV" sex="m">Bob</user>
<user state="CA" sex="m">Jon</user>
<user state="AZ" sex="m">Jim</user>
<user state="OR" sex="f">Joy</user>
<user state="NV" sex="f">Amy</user>
</users>
Using jQuery, is there a way to select users who are male and are either from CA or NV, but without using the filter function? To be clear, I know that
$(xml).find("user[sex='m']")
selects only male users, while
$(xml).find("user[state='CA'],[state='NV']")
selects all users from either CA or NV. But I am not able to combine both of them with a logical AND within a single selector.
Using the filter function, however, the following works:
$(xml).find("user").filter(function() {
return $(this).attr('sex') == 'm' && ($(this).attr('state') == 'CA' || $(this).attr('state') == 'NV')
}).each(function() {
alert($(this).text());
});
Thanks!
Share Improve this question edited Dec 10, 2010 at 2:14 BoltClock 723k165 gold badges1.4k silver badges1.4k bronze badges asked Dec 10, 2010 at 1:57 user537264user537264 931 silver badge3 bronze badges2 Answers
Reset to default 21Try this:
$(xml).find("user[sex='m'][state='CA'], user[sex='m'][state='NV']")
Basically you chain the sex
and state
attributes together in a single simple selector (this would be your logical AND), and repeat them once per state (and this would be your logical OR).
Test:
$(xml).find("user[sex='m'][state='CA'], user[sex='m'][state='NV']")
.each(function() {
alert($(this).text() + " - " + $(this).attr('state'));
});
Output:
Max - CA
Bob - NV
Jon - CA
You can combine multiple selectors, but your syntax isn't exactly correct:
$(xml).find("user[state='CA'],[state='NV']")
This finds all <user> elements with the state "CA" and any other nodes (not necessarily <user>) with state "NV". What you want is:
$(xml).find("user[state=CA][sex=m],user[state=NV][sex=m]")
If you don't want to repeat yourself:
$(xml).find("user").filter("[state='CA'],[state='NV']").filter("[sex='m']");
版权声明:本文标题:javascript - How do I combine logical OR with logical AND within a jQuery attribute selector? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738730296a2109284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论