[英]How to fill HTML list in a loop
我有一个HTML列表,我从REST调用中以JSON字典的形式接收数据,遍历并获取其值。 但是我找不到如何使用循环生成下面的HTML。
<div class="container">
<ul role="list">
<li class="infoBasic" role="listitem">
<div class="date">20-12-2012</div>
<div class="ammount">-€4,25</div>
</li>
<li class="infoDetails" role="listitem">
<div class="place">data</div>
<div class="category">data</div>
<div class="description_title">data</div>
<div class="description">data</div>
</li>
//repeat this for multi items received from REST
</ul>
$.ajax
({
success: function (data){
var transactions=data['transactions'];
$.each( transactions, function( key, value ) {
$.each( value, function( key, value) {
//here how to set/create the appropriate div elements(like above)
// with the values I get here in a loop?
});
});
编辑下面的一些解决方案很好,但是我不希望从对象数组中获得所有值,我如何只将这些项目(日期,金额,位置..etc)获取到html div中,而忽略字典中的其他项目
$.each( value, function( key, value) {
//here how to set/create the appropriate div elements(like above)
// with the values I get here in a loop?
$(".infoDetails").append("<div class='" + key + "'>" + value + "</div>");
});
您可以使用..
var data = "";
$.each( transactions, function( key1, value1 ) {
data +="<li class ='"+ key1 +"' role='listitem'>";
$.each( value1, function( key, value) {
data +="<div class ='"+ key +"'>"+value+"</div>";
});
});
$("ul").html(data); // selecting ul element and then embedding html into it..
试试这个...采取一个变量说html ...遍历json数组...在列表中添加项目...并将整个html放入div ..
var html='<ul>';
$.ajax
({
success: function (data){
var transactions=data['transactions'];
$.each( transactions, function( key, value ) {
$.each( value, function( key, value) {
html+='<li class="'+key+'">'+value+'</li>';
});
});
}
});
html+='</ul>';
$('#idofdiv').html(html);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.