Find elements in an xml dom where two attribute correspond to certain values using jQuery?
I have:
<drug_list>
<drug id='4' supplier_id='4'></drug>
<drug id='1' supplier_id='2'></drug>
<drug id='21' suppl开发者_开发问答ier_id='45'></drug>
<drug id='1' supplier_id='7'></drug>
</drug_list>
How do i use jQuery to get the drug with id of "1" and supplier_id of "2"?
For reference see: this
//Do whatever to get the xml (simulating getting from somewhere here).
var $xml = $("<drug_list><drug id='4' supplier_id='4'>abc</drug><drug id='1' supplier_id='2'>def</drug></drug_list>");
//make sure to pass the xml as context to the selector (2nd param).
$("drug[id='1'][supplier_id='2']", $xml).each(function() {
$this = $(this);
//do whatever you want with $this i.e. $this.text() or $this.attr("id")...
});
try this....
$('drug').each(function(){
var id = $(this).attr('id');
var supplier_id = $(this).attr('supplier_id');
if(id==1 && supplier_id==2) {
///do something
}
});
精彩评论