我的jquery代码获取名称,代码和类别:
$(req.responseText).find("Table").each(function(i) {
var item = $(this),
name = item.find('Name').text(),
code = item.find('Code').text(),
catg = item.find('Category').text();
var $content = $('<li><a href="#"><img src="../../img/album-bb.jpg"><h3>Name: '+ name + '</h3><p>Code: '+ code + '</p><p>Category: '+ catg + '</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');
});
这是我对XML的示例回复:
<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>
listview输出是这样的:
Name: Blackened red snapperCelerySoupChallah French toast
Code: 106377303570303675
Category: 123123123
如何放入不同的listview?..当它的append.Thanks
答案 0 :(得分:1)
试试这个:
var ni = 0;
var ci = 0;
var cati = 0;
var NameArr = new Array();
var CodeArr = new Array();
var CatArr = new Array();
$(req.responseText).find('Name').each(function () {
NameArr[ni] = $(this).text();
ni++;
}
$(req.responseText).find('Code').each(function () {
CodeArr[ci] = $(this).text();
ci++;
}
$(req.responseText).find('Category').each(function () {
CatArr[cati] = $(this).text();
cati++;
}
for(var i=0; i<NameArr.length; i++){
var $content = $('<li><a href="#"><img src="../../img/album-bb.jpg"><h3>Name: '+ NameArr[i] + '</h3><p>Code: '+ CodeArr[i]+ '</p><p>Category: '+ CatArr[i]+ '</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');
}
答案 1 :(得分:1)
工作示例:http://jsfiddle.net/Gajotres/uzphF/
$('#index').live('pagebeforeshow',function(e,data){
$('#table-data').empty();
$.ajax({
type: "POST",
url: "/echo/xml/",
dataType: "xml",
data: {
xml: '<Tables><Table><Code>106377</Code><Name>Blackened red snapper</Name><Category>123</Category><Yield>4</Yield><YieldUnit/></Table><Table><Code>303570</Code><Name>Celery soup</Name><Category>123</Category><Yield>1</Yield><YieldUnit/></Table><Table><Code>303675</Code><Name>Challah French Toast</Name><Category>123</Category><Yield>6</Yield><YieldUnit/></Table></Tables>'
},
success: function(xml) {
ajax.parseXML(xml);
},
error: function (request,error) {
alert('Network error has occurred!');
}
});
});
var ajax = {
parseXML:function(result){
$(result).find("Table").each(function(){
var code = $(this).find('Code').text();
var name = $(this).find('Name').text();
var cat = $(this).find('Category').text();
var yield = $(this).find('Yield').text();
$('#table-data').append('<li><a href="#cars"><h3>Table code:<span> '+code+'</span></h3><p>Table name: ' + name + '</p></a></li>');
});
$('#table-data').listview('refresh');
}
}
这个例子使用了mockup jsFiddle函数来测试XML ajax调用,但这部分并不重要。您唯一需要知道的是如何循环遍历XML。还有一件事,您的XML示例必须包含在父标记中,我将其命名为 tables 。