0

XML 响应:

<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
    <Code>106377</Code>
    <Name>Blackened red snapper</Name>
    <Category>123</Category>
    <Yield>4</Yield>
    <YieldUnit/>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
    <Code>303570</Code>
    <Name>Celery soup</Name>
    <Category>123</Category>
    <Yield>1</Yield>
    <YieldUnit/>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
    <Code>303675</Code>
    <Name>Challah French Toast</Name>
    <Category>123</Category>
    <Yield>6</Yield>
    <YieldUnit/>
</Table>
<Table diffgr:id="Table4" msdata:rowOrder="3">
    <Code>303681</Code>
    <Name>Challah french toast</Name>
    <Category>123</Category>
    <Yield>4</Yield>
    <YieldUnit/>
</NewDataSet>

HTML 代码

<div data-role="output">
    <ul id="RecipeList" data-role="listview" data-split-icon="star" data-split-theme="e" data-inset="true">
    </ul>
</div>

JS 代码

var CodeObj = new Array();
var NameObj = new Array();
var Codeindex = 0;
var Nameindex = 0;

$(req.responseText).find('Name').each(function () {
    NameObj[Nameindex] = $(this).text();
    Nameindex += 0;

    for (var i = 0; i < NameObj.length; i++) {
        $(this).append(NameObj[i] + "<br/>");

        $(req.responseText).find('Code').each(function () {
            CodeObj[Codeindex] = $(this).text();
            Codeindex += 0;
            for (var a = 0; a < CodeObj.length; a++) {
                $(this).append(CodeObj[a] + "<br/>");
            }
        });

        var $content = $('<li><a href="#"><img src="../../img/album-bb.jpg"><h3>Name: ' + NameObj[i] + '</h3><p>Code: ' + CodeObj[a] + '</p></a><a href="#purchase" data-rel="popup" data-position-to="window" data-transition="pop">Add to favorites</a></li>');
        $('#RecipeList').append($content).listview('refresh');
    }
});

列表视图输出

Name : Blackened red snapper
Code : 106377
Name : Celery soup
Code : 106377
Name : Challah french toast
Code : 106377
Name : Challah french toastr
Code : 106377

问题:总是相同的代码。谁能知道我的循环有什么问题?谢谢你。我尝试了很多方法。但仍然可以获得名称的正确输出

4

3 回答 3

1

问题在于增量。代替:

Nameindex +=0;
Codeindex +=0;

Nameindex++;
Codeindex++;

或者

Nameindex +=1;
Codeindex +=1;
于 2013-04-25T02:23:29.290 回答
0

似乎

名称索引 +=0; 代码索引 +=0;

错了。不应该是Nameindex+=1吗?

于 2013-04-25T02:21:56.997 回答
0

Nameindex += 0并且Codeindex += 0是无操作的,所以他们可能与它有关。

相反,试试这个:

$(req.responseText).find("Table").each(function() {
    var item = $(this),
        name = item.find("Name").text(),
        code = item.find("Code").text();
    $("#RecipeList").append('<li><a href="#"><img src="...." />'
       +'<h3>'+name+'</h3>Code: '+code+'</a>.....');
});
于 2013-04-25T02:22:27.120 回答