2013-01-22 27 views
-2

我的XML文件:
獲取子屬性使用值的任何客戶端語言

<childrens> 
     <child1 entity_id = "1" value = "Asia"> 
       <child2 entity_id = "2" value = "India"></child2> 
       <child3 enity_id = "3" value = "China"></child3> 
     </child1> 
</childrens> 

這是我的代碼:

<script> 
    $(function() { 
     $('#update-target a').click(function() { 
      $.ajax({ 
       type: "GET", 
       url: "try.xml", 
       dataType: "xml", 
       success: function(xml) { 
        $(xml).find('child1').each(function(){ 
         var value_text = $(this).attr('value') 
         $('<li></li>') 
          .html(value_text) 
          .appendTo('#update-target ol'); 
        }); //close each(
       } 
      }); //close $.ajax(
     }); //close click(
    }); //close $(
    </script> 
    </head> 
    <body> 
    <p> 
     <div id='update-target'> 
     <a href="#">Click here to load value</a> 
     <ol></ol> 
     </div> 
    </p> 

我想輸出,如果我在文本輸入亞洲盒比使用任何客戶端腳本語言顯示印度和中國

+0

爲什麼是中國印度的孩子? –

+0

其實中國是印度的一個孩子在xml中。 – SHANK

+0

你想要什麼樣的「輸出」 - HTML中的某些東西添加到頁面上的某個元素上?你試過了什麼,你卡在哪裏? – LarsH

回答

1

要獲得JavaScript值(myXml =文件,如果它是在HTML文件):

var value = myXml.getElementById('1').getAttribute('value'); 

使用jQuery:

var value = $('#1').attr('value'); 
+0

我想他希望根據在文本框中輸入的文本值「亞洲」來獲取亞洲元素,而不是基於其ID。 – LarsH