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