遍历jQuery列表中的JSON数组 [英] looping through JSON array in a jQuery list
本文介绍了遍历jQuery列表中的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个JSON数组
// Json array
var productList = {"products": [
{"description": "product 1", "price": "9.99"},
{"description": "product 2", "price": "9.99"},
{"description": "product 3", "price": "9.99"}
]
};
我希望它在我的列表视图中循环,但不知道如何执行此操作 到目前为止,我所能做的就是一次列出一项.另外,我只能列出产品,而不能列出产品=价格. jQuery论坛实例正在帮助...谢谢!!
I want it to loop through my list view but have no idea how to do this all I can do so far is list one item at a time. Also, I can only list the product and not the product = the price. The jQuery forum inst helping... thanks !!
这是其余的代码
function loadList() {
// var list = document.getElementById('productList');
var list = $("#productList").listview();
var listItem = document.createElement('li');
listItem.setAttribute('id', 'listitem');
listItem.innerHTML = productList.products[0].description;
$(list).append(listItem);
$(list).listview("refresh");
和HTML文件
<html xmlns:f="http://www.lipso.com/f" xmlns:l="http://www.lipso.com/v2/lml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<head>
<title>Page Title</title>
&meta;
<script src="@=site.cfg.resources.url@/test.js"></script>
</head>
<body onLoad="loadList()">
<div data-role="page">
<div data-role="header" id="header">
<h1>Dynamic Product List</h1>
</div>
<div data-role="content" id="content">
<ul id="productList" data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b">
</ul>
</div>
</div>
</body>
</html>
推荐答案
由于您已经在使用jQuery,为什么不使用 $.each()函数?
Since you're already using jQuery, why don't you use the $.each() function?
代替此代码:
var listItem = document.createElement('li');
listItem.setAttribute('id', 'listitem');
listItem.innerHTML = productList.products[0].description;
$(list).append(listItem);
使用此:
$(productList.products).each(function(index){
$(list).append('<li id="listitem">' + this.description + " = " + this.price + '</li>');
});
这篇关于遍历jQuery列表中的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文