1

我的 xml 看起来像这样:

<oneday>
      <team1 id="1" team="India">
           <team2 id="2" team="gujarat">
                  <team3 id="3" team="guj11"></team3>
           </team2>
      </team1>
</oneday>

这是我的代码我做了什么?:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var xml;
$.get(
    "cricket.xml",  
    function(data) { xml=data; },
    "html"
);
function get_list(){

    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find($("#select").val());
    $("#result").html($title.text());
}
</script>
</head>
<input type="text" id="select">
<input type="button" name="button" value="Search" onclick="get_list()" >
<div id="result">
</div>
</html>

我想输出:
如果我在文本框中输入印度然后显示古吉拉特和 guj11
输入古吉拉特然后输出是 Guj11

4

3 回答 3

0
$title = $xml.find($("#select").val());

您正在 $xml 对象中找到“#select”的值。这是错误的

你可以像这样解析你的xml..

$team1 = $(xmlDoc).find("team1");
alert($team1.attr("id"));
alert($team1.attr("team"));
于 2013-01-28T06:28:23.817 回答
0

您可以找到如下输入项:

$title = $xml.find('[team="'+$('#select').val()+'"]');

然后找到他的孩子使用:

$nodes = $title.find('*');

这是我的新get_list功能:

function get_list(){
  xmlDoc = $.parseXML( xml ),
  $xml = $( xmlDoc ),
  $title = $xml.find('[team="'+$('#select').val()+'"]');
  $nodes = $title.find('*');
  var result='';
  $nodes.each(function(){
    result += $(this).attr('team');
    result += ' ';
  });
  $("#result").html(result);
}

jsfiddle 在这里http://jsfiddle.net/g9mYk/

于 2013-01-28T06:43:50.717 回答
0

试试这个

function get_list(){
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find('[team="'+$('#select').val()+'"] *');
    var str='';
    $title.each(function(){
        str+=$(this).attr('team')+' ';
    });
    $("#result").html(str);
}

您也可以更改您的 xml 以方便您使用,例如

<oneday>
      <team id="1" team="India">Test
           <team id="2" team="gujarat">
                  <team id="3" team="guj11">Hello</team>
           </team>
      </team>
</oneday>

因为每个标签 team1、team2、team3 都定义了 id。

于 2013-01-28T06:59:26.020 回答