在xml dom中查找两个属性使用jQuery对应于某些值的元素?

时间:2011-01-18 10:25:15

标签: jquery xml

我有:

<drug_list>
  <drug id='4' supplier_id='4'></drug>
  <drug id='1' supplier_id='2'></drug>
  <drug id='21' supplier_id='45'></drug>
  <drug id='1' supplier_id='7'></drug>
</drug_list>

如何使用jQuery获取ID为“1”且supplier_id为“2”的药物?

2 个答案:

答案 0 :(得分:2)

参考: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")...
  });

答案 1 :(得分:0)

试试这个......

$('drug').each(function(){
var id = $(this).attr('id');
var supplier_id = $(this).attr('supplier_id');
if(id==1 && supplier_id==2)  {
///do something
}  
});