jQuery的每个JSON对象
我有一个结果集从服务返回,让我下面的JSONjQuery的每个JSON对象
{
"ThreadCount":61,
"GroupList":[
{"userName":"KLT","count":2687},
{"userName":"KCameron","count":718},
{"userName":"IRG","count":156},{"userName":"JLB","count":123},{"userName":"HML","count":121},{"userName":"SMN","count":99},{"userName":"TBridges","count":68},{"userName":"ARF","count":65},{"userName":"MarkGreenway","count":61},{"userName":"SMC","count":55},{"userName":"EMD","count":52},{"userName":"PKP","count":41},{"userName":"KBounds","count":36},{"userName":"MAN","count":33},{"userName":"LAC","count":17},{"userName":"EPS","count":17},{"userName":"CAN","count":7},{"userName":"MAJ","count":3},{"userName":"CPC","count":2}]
}
我想使用jQuery(或JavaScript把THREADCOUNT在一个DIV,并添加用户名和counds为表
success: function(result) {
$("#Unfiled").html(result.ThreadCount);
$.each(result.GroupList, function(user) {
$('#myTable > tbody').append(
'<tr><td>'
+ user.userName
+ '</td><td>'
+ user.count +
'</td></tr>'
);
});
}
出于某种原因,我没有得到我的表什么...
顺便说一句我的HTML是在这里:
<table>
<tr>
<td>
Unfiled Emails:
</td>
<td id="Unfiled">
-1
</td>
</tr>
<tr>
<td colspan="2">
<table id="myTable" border="2" cellpadding="3" cellspacing="3">
</table>
</td>
</tr>
</table>
我知道我失去了一些东西简单...
感谢您的帮助提前
在提供给each
的函数中,this
引用了当前元素。试试这个:
$.each(result.GroupList, function() {
$('#myTable > tbody').append(
'<tr><td>'
+ this.userName
+ '</td><td>'
+ this.count +
'</td></tr>'
);
});
如果不为你工作,它可能有一些与此有关:$('#myTable > tbody')
,考虑到没有tbody
元素。我相信Internet Explorer会自动创建一个,但其他浏览器不会。查看$.support.tbody
以查看浏览器是否为您提供帮助。
对于我的解决方案,我用你的功能改变来使它工作。但更改表格的html确实为我加快了速度。 – MarkKGreenway 2009-10-15 05:27:01
@nickf'这'解决了我的问题 – jeezyfreezy 2014-01-12 18:29:11
当我用$。每()我使用了一个函数(I,项目),其中我是一个指示索引的整数,item是实际的对象。这就是the documentation显示它正在完成 - 该方法被描述为函数回调(indexInArray,valueOfElement)。
我注意到你的表并没有实际的tbody元素。这可能是你问题的一部分。
$('#myTable > tbody').append.....
<table id="myTable" border="2" cellpadding="3" cellspacing="3">
</table>
我也建议您在$。每()循环创建一个字符串,然后做你的每一个循环之后的以下内容:
$('#myTable > tbody').html(string);
这将减少每次追加的开销你遍历数组。
您是否完全确定您收到了您所描述的答复?我注意到,你有没有填充计数器-1而不是61,好像服务器返回你错误的数据。 – 2009-10-15 00:00:43
抱歉误导你Artem ... -1是阿贾克斯发生之前有什么。 – MarkKGreenway 2009-10-15 05:12:08