2014-12-04 63 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