2012-05-15 25 views
0

我收到使用查詢服務器時,以下REST:的jQuery:獲得子元素的值,而其他子元素相匹配的字符串

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<widgets> 
    <widget> 
    <wid>wid1007</wid> 
    <path>widgets-1007</path> 
    <name>Frobnutter</name> 
    <id>1</id> 
    <version>0</version> 
    </widget> 
    <widget> 
    <wid>wid1008</wid> 
    <path>widgets-1008</path> 
    <name>Roberts-Coupler</name> 
    <id>2</id> 
    <version>0</version> 
    </widget> 
</widgets> 

我需要從一個「小部件」提取「婦女參與發展」的值使用已知的「id」值。這在jQuery中看起來像什麼?

謝謝!

回答

0
var xml = //your xml output 

$(xml).find('wid').each(function() { 
    var content = $(this).text(); 
    var id = $(this).siblings('id').text(); 
    //do something with each <wid> tags content 
}); 

$('widget', xml).filter(function() { 
    if ($(this).find('id').text() === '1') return $(this); 
}).find('wid'); 
0

將XML給一個變量可以說rest_data

$(rest_data).find("widget").each(function() 
{ 
    if($(this).find("id").text() == "2") 
    { 
    //do something 
    alert($(this).find("wid").text()); 
    } 
}); 
相關問題