开发者

get subchild value for a specific child in xml via jquery

I'am a newbie in jquery and I want to parse subchilds in a xml file for a specific child attribute, for example, I have this cities and districts list

<city name="ANKARA"> 
  <distr>BEYPAZARI</distr>
  <distr>GÜDÜL</distr>
  <distr>KAZAN</distr>
  <distr>ÇANKAYA</distr>
</city>
<city name="İSTANBUL">
 <distr>EMİNÖNÜ</distr>
 <distr>ÇATALCA</distr>
 <dist开发者_开发百科r>BEYOĞLU</distr>
 <distr>BEYKOZ</distr>
 <distr>BEŞİKTAŞ</distr>
</city>

I use this code to get city list

 $(xml).find('city').each(function(){
     var city = $(this).attr("name");
     $("<option>").text(city).appendTo("#cityList");
 }); 

but I don't how to get the district list of for example Ankara, someone could help me please thanks in advance


 $(xml).find('city[name="ANKARA"]').each(function(){
     var city = $(this).attr("name");
     $("<option>").text(city).appendTo("#cityList");
 }); 

That's the "attribute equals" selector you want.


Try this to iterate through all the city and the child districts

$(xml).find('city').each(function(){
     var city = $(this).attr("name");
     $('distr', this).each(function(){
       //Add your code here to deal with Districts
    }
   });
   $("<option>").text(city).appendTo("#cityList");
 }); 


both answers above helped me to get exactly what I want,

$(xml).find('city[name="ANKARA"]').find('distr').each(function(){
    var distr = $(this).text();
    $("<option>").text(distr).appendTo("#district_list");
});

thanks a lot

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜