jQuery JSON .each 错误
我有一个如下所示的 JSON 对象。
[
{
"id" : "23",
"event_id" : "0",
"sport_title" : null,
"event_title" : null,
"title" : "Under 1 day!",
"content" : "It\\'s all hotting up and battle commences in under one day!",
"link" : ""
},
{
"id" : "20",
"event_id" : "0",
"sport_title" : null,
"event_title" : null,
"title" : "Getting Exciting",
"content" : "Less than two days till it all kicks off, with cricket....",
"link" : ""
}
]
我试图从这个 JSON 对象中获取详细信息,并将它们添加到
前面,我当前正在尝试的代码看起来像这样,并且遇到问题
var writeup_list = writeuplist;
$.getJSON('../json/getWriteups.json', function(data) {
$.each(data.items, function(i, item) {
writeup_list.html(function() {
var output;
output = '<li><a href="/writeups/index.php#writeup_' + item.id + '">';
if(item.sport_title != null) {
output += item.sport_title + ' - ';
}
output += item.title + '</a></li>';
return output;
});
});
});
writeuplist 只是 ul 对象。
我还担心会覆盖列表中已有的信息或只是再次添加。不想继续添加相同的数据。有什么好方法可以避免这种情况呢?
我似乎在从 JSON 文件中获取数据时遇到问题,我相信这与我尝试在 .each 函数中访问它的方式有关。
谁能发现哪里出了问题吗?
I have a JSON object that looks like this.
[
{
"id" : "23",
"event_id" : "0",
"sport_title" : null,
"event_title" : null,
"title" : "Under 1 day!",
"content" : "It\\'s all hotting up and battle commences in under one day!",
"link" : ""
},
{
"id" : "20",
"event_id" : "0",
"sport_title" : null,
"event_title" : null,
"title" : "Getting Exciting",
"content" : "Less than two days till it all kicks off, with cricket....",
"link" : ""
}
]
and I am trying to get the details out of this JSON Object and prepend them to a <ul>
the code that I am currently trying looks like this and is having problems
var writeup_list = writeuplist;
$.getJSON('../json/getWriteups.json', function(data) {
$.each(data.items, function(i, item) {
writeup_list.html(function() {
var output;
output = '<li><a href="/writeups/index.php#writeup_' + item.id + '">';
if(item.sport_title != null) {
output += item.sport_title + ' - ';
}
output += item.title + '</a></li>';
return output;
});
});
});
writeuplist is just the ul object.
I am also worried about overriding information that is already in the list or just adding again. Don't want to keep adding the same data. What is a good way to avoid this?
I seem to be having a problem in getting the data out of the JSON file I believe it has something to do with the way am trying to access it in the .each function.
Can anyone spot where am going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要注意的一些事项:
alert(data.items)
,并确保您看到[object Array])。item.sport_title
将为空吗?我更改了它以检查其字符串...element.html(str)
将替换当前内容。您的示例在每次迭代时都会替换当前的 html,因此最后,您只会拥有列表中最后一个列表元素的内容。Some things to note:
alert(data.items)
in the callback, and make sure you see [object Array]).item.sport_title
will be null? I changed it to check its a string...element.html(str)
will replace the current contents. Your sample was replacing the current html with each iteration, so at the end, you'd only have the contents of the last list element in your list.首先,您应该只使用
data
,而不是data.items
。在您的情况下,返回的数据是一个数组,因此您需要对其进行迭代。其次,您的代码编写方式会不断覆盖
writeup_list
的内容。相反,构建 html 字符串,然后将其设置在末尾:First of all, instead of
data.items
, you should just usedata
. In your case the data returned is an array, so you want to iterate over that.Second, the way your code is written, it will keep overwriting the contents of
writeup_list
. Instead, build up the html string and then set it at the end:我相信你应该使用:
但是你也可以使用一个标准的javascript循环来做同样的事情:
另外,你不想使用
.html()
来设置你的lis,因为这会覆盖html 已经就位。使用.append()
代替:I believe you should use:
But you can also use a standard javascript loop which does the same thing:
Also, you don't want to use
.html()
to set your lis since that will overwrite the html already in place. Use.append()
instead: