0

这是我的arty.xml

<child_2 entity_id="2" value="Root" parent_id="1">
    <child_4 entity_id="4" value="Activities" parent_id="2">
        <child_10066 entity_id="10066" value="Physical1" parent_id="4">
            <child_10067 entity_id="10067" value="Cricket" parent_id="10066">
                <child_10068 entity_id="10068" value="One Day" parent_id="10067"/>
            </child_10067>
        </child_10066>
        <child_10069 entity_id="10069" value="Test2" parent_id="4"/>
        <child_10070 entity_id="10070" value="Test3" parent_id="4"/>
        <child_10071 entity_id="10071" value="Test4" parent_id="4"/>
        <child_10072 entity_id="10072" value="Test5" parent_id="4"/>
        <child_5 entity_id="5" value="Physical" parent_id="4"/>
    </child_4>
</child_2>

这是我尝试阅读它:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<title>Using jQuery and XML to populate a drop-down box demo</title>
<script>
$(document).ready(function(){
    $.ajax({
    type: "GET",
    url: "atry.xml",
    dataType: "xml",
    success: function(xml) {
    var select = $('#mySelect');

    $(xml).find('child_2').each(function(){
    $(this).find('child_4').each(function(){
    var value = $(this).attr('value');
    select.append("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
        });
    });
        select.children(":first").text("Select").attr("selected",true);
} //sucess close
}); 
}); 
</script>
</head>
<body>    <div id="page-wrap">
<select id="mySelect">
<option>loading</option>
</select>
</div>
</body>
</html>

在这里,我正在尝试加载所有属性值并创建选择列表...

但问题是我没有得到所有孩子的价值观......

帮我摆脱这个...

4

3 回答 3

1

嗨,我在本地测试过它工作正常

可能是您的问题是您正在使用 $(this).attr('value');

所以你可以这样尝试

$(document).ready(function(){
        $.ajax({
        type: "GET",
        url: "atry.xml",
        dataType: "xml",
        success: function(xml) {
        var select = $('#mySelect');

         $(xml).find('child_2').each(function(){
         var value = $(this).find('child_4').attr('value');
         select.append("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
          });
});
于 2013-04-01T06:45:02.527 回答
0

您可以像这样获取所有子标签值。

 $(document).ready(function(){
        $("xml").find('child_2').each(function(){
        $(this).children().map(function(){
            $("#mySelect").append("<option class='ddindent' >"+$(this).attr('value')+"</option>");

            var tag = this.tagName;

            $(tag).children().map(function(){
                //alert("alert : " + this.tagName + " value : " + $(this).attr("value"));
                $("#mySelect").append("<option class='ddindent' >"+$(this).attr('value')+"</option>");
            });
            });
        });
            //select.children(":first").text("Select").attr("selected",true);
    } //sucess close

    ); 
    </script>

希望它对你有帮助:)

于 2013-04-01T07:39:59.693 回答
0

我认为你不必再次循环,你可以试试这个:

$(xml).find('child_2').each(function(){
   $(this).find('child_4').children().find('[entity_id]').each(function(){
       var value = $(this).attr('value');
       select.append("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
   });
});
于 2013-04-01T06:36:31.027 回答