2014-12-04 61 views
0

我想在選擇程序節點後從父節點獲取值,下面是加載XML的ajax函數,並找到每個存儲在計劃數組。使用JQuery在XML中從父節點獲取值

 $.ajax({ 
       type: "GET", 
       url: "tvguide.xml", 
       success: function(xml){ 

        //for each schedule ID stored in the array this function fires 
        $.each(schedule_array, function(index, item) { 
         //finds the program ID and then inserts the data from the XML into a UL in the Series Details div 
         $(xml).find("programme[id="+item+"]").each(function(){ 
          //Get the value from the previous node and store here? 
         }); 
        }); 
       }, 
       error: function(){ 
        alert("XML File could not be found."); 
       } 
      }); 

下面是XML的一個小例子。

<channel value="channel_1"> 
     <programme id="1"> 
      //programmes details in here 
     </programme> 
</channel> 

基本上我需要獲得通道節點的值,當在上面的循環中選擇了該通道中的程序時。這可能嗎?

回答

1

您可以使用.parent()讓家長參考,並.attr()獲取/設置屬性值:

$(xml).find("programme[id="+item+"]").each(function(){ 
    var parentval = $(this).parent().attr('value'); 
}); 
+0

AHHHHHHHH!非常感謝你,在過去的幾個小時裏,我幾乎一直在爲此撓頭。這是我第一次使用AJAX和XML,所以很抱歉,如果問題很愚蠢,但非常感謝! – Rafty 2014-12-04 12:08:01

+0

很高興幫助阿蘭:) – 2014-12-04 12:10:15