gpt4 book ai didi

jquery - 循环遍历 json 结果以创建列表

转载 作者:行者123 更新时间:2023-12-01 06:37:01 25 4
gpt4 key购买 nike

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

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

JQUERY

$.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>");
});

最佳答案

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

// 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>");
});
}

关于jquery - 循环遍历 json 结果以创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12199321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com