1

我使用 $.post() 检索 json 结果,我希望能够打印到每个结果的列表中。

使用以下仅显示最后一个 json 项

查询

$.post('/assets/inc/account-info.php', qString, function (data) {
    console.log(data);
    var datas = jQuery.parseJSON(data);
    console.log(datas);
    $("#note").append("<li>id=recordsarray_"+datas.id+"><a href='#"+datas.id+"'>"+datas.name+"</a></li>");  
});
4

4 回答 4

2

按索引遍历数据并设置,然后获取每个数据的 ID 和名称。我还假设(因为您说过列表)您的数据有多个级别。

$.each(datas, function() {
    $.each(this, function(key, value) {
        $("#note").append("<li id=recordsarray_" + 
             datas[key].id + "><a href='#" + 
             datas[key].id + "'>" + 
             datas[key].name + "</a></li>"
        );
    });
});
于 2012-08-30T14:37:17.727 回答
2

我假设您在前端获取对象数组,并且该对象中有 id 和 name:

// To put data directly in html you can do this :
    $.each(datas,function(i,data){
       $("#note").append("<li>id=recordsarray_"+data.id+"><a href='#"+data.id+"'>"+data.name+"</a></li>");
    });

 OR

// To Put your data in array you can do this :
    var arr = [];
    // This will help you putting the json object in to array
    $.each(datas,function(i,data){
       arr.push({id:data.id, name:data.name});   
    });

    // once you get the array you can loop through it and add it to your html:
    for (i=0;i<arr.length;i++){ 
        $("#note").append("<li>id=recordsarray_"+arr[i].id+"><a href='#"+arr[i].id+"'>"+arr[i].name+"</a></li>");
    });   
    }
于 2012-08-30T15:02:31.100 回答
1
$(datas).each(function(){ $("#note").append("<li>id=recordsarray_"+this.id+"><a href='#"+this.id+"'>"+this.name+"</a></li>"); });
于 2012-08-30T14:37:29.720 回答
1

试试这个

$.each(datas, function(i,item){
    $("#note").append("<li>id=recordsarray_"+datas[i].id+"><a href='#"+datas[i].id+"'>"+datas[i].name+"</a></li>");
});
于 2012-08-30T14:40:49.303 回答