Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用变量名查找xml属性值_Javascript_Xml - Fatal编程技术网

Javascript 使用变量名查找xml属性值

Javascript 使用变量名查找xml属性值,javascript,xml,Javascript,Xml,xml文件:- <countries> <country code='af' phoneCode='93' name='Afghanistan' /> <country code='al' phoneCode='355' name='Albania' /> <country code='dz' phoneCode='213' name='Algeria' /> <country code='ad' phoneCode='376

xml文件:-

<countries>
  <country code='af' phoneCode='93' name='Afghanistan' />
  <country code='al' phoneCode='355' name='Albania' />
  <country code='dz' phoneCode='213' name='Algeria' />
  <country code='ad' phoneCode='376' name='Andorra' />
  <country code='ao' phoneCode='244' name='Angola' />
  <country code='aq' phoneCode='672' name='Antarctica' />
  <country code='ar' phoneCode='54' name='Argentina' />
  <country code='am' phoneCode='374' name='Armenia' />
  <country code='aw' phoneCode='297' name='India' />
  <country code='au' phoneCode='61' name='Australia' />
</countries>

这是我的JavaScript ajax:-

<script>
    var getr='';
    var ind= "India";
    var getre;
        function cli(){
            $.ajax({ 
                url: 'country_code.xml', 
                type:"get",
                async: false,
                success: function(xml) { 
                       var result = $(xml).find("countries").each(function(){
                           getr =  $(this).find("country").attr('name');
                           });
                        alert(getr);
                },
                error:function(){
                    alert('err');
                }
            });
        }
    </script>

var getr='';
var ind=“印度”;
var getre;
函数cli(){
$.ajax({
url:“country_code.xml”,
键入:“获取”,
async:false,
成功:函数(xml){
var result=$(xml).find(“国家”).each(函数(){
getr=$(this.find(“country”).attr(“name”);
});
警报(getr);
},
错误:函数(){
警惕(“错误”);
}
});
}
我想在这里搜索属性
印度

我在上面尝试过,但它先给我
阿富汗

我怎样才能做到这一点。

谢谢。

也许这正是你想要做的:
搜索属性名称等于var ind的国家/地区条目。 在本例中,将返回电话代码

success: function(xml) { 
               var result = $(xml).find("country").each(function(){
                   if( $(this).attr('name') == ind) {
                        getr = ind + " :" + $(this).attr('phoneCode') 
                   }
                   });
                alert(getr);
        },

您正在迭代每个
country
节点,并系统地覆盖
getr
变量。第一个是阿富汗,超越循环的价值将是澳大利亚。我不明白你想做什么。如果
getr
的值为India,是否返回India。。?