Jquery 每个 Json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1569372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Each Json Object
提问by MarkKGreenway
I have a result set returning from a service that gives me the following json
我有一个从服务返回的结果集,该服务为我提供以下 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}]
}
I want to use Jquery (or javascript to put the threadCount in one div and add the usernames and counds to a Table
我想使用 Jquery(或 javascript 将 threadCount 放在一个 div 中并将用户名和密码添加到表中
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>'
);
});
}
For some reason I am not getting anything in my table...
出于某种原因,我的桌子上没有任何东西......
By the way my HTML is here :
顺便说一下,我的 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>
I know i am missing something simple...
我知道我错过了一些简单的东西......
Thanks for your help in advance
提前感谢您的帮助
回答by nickf
Inside the function provided to each
, this
refers to the current element. Try this:
在提供给 的函数内部each
,this
指的是当前元素。尝试这个:
$.each(result.GroupList, function() {
$('#myTable > tbody').append(
'<tr><td>'
+ this.userName
+ '</td><td>'
+ this.count +
'</td></tr>'
);
});
If that doesn't work for you, it may have something to do with this: $('#myTable > tbody')
, considering that there is no tbody
element. I believe that Internet Explorer will automatically create one but the other browsers won't. Check $.support.tbody
to see if the browser does that for you.
如果这对您不起作用,则可能与此有关:$('#myTable > tbody')
,考虑到没有tbody
元素。我相信 Internet Explorer 会自动创建一个,但其他浏览器不会。检查$.support.tbody
浏览器是否为您执行此操作。
回答by Corey Ballou
I noticed your table does not actually have a tbody element. This could be part of your issue.
我注意到你的表实际上没有 tbody 元素。这可能是您问题的一部分。
$('#myTable > tbody').append.....
<table id="myTable" border="2" cellpadding="3" cellspacing="3">
</table>
I would also suggest that you create a string in your $.each() loop and then do the following after your each loop:
我还建议您在 $.each() 循环中创建一个字符串,然后在每个循环后执行以下操作:
$('#myTable > tbody').html(string);
This will reduce the overhead of appending each time you iterate over the array.
这将减少每次迭代数组时追加的开销。
回答by Kaleb Brasee
When I've used $.each() I have used a function(i, item), where i is an integer indicating index, and item is the actual object. That is how the documentationshows it being done -- the method is described as function callback(indexInArray, valueOfElement).
当我使用 $.each() 时,我使用了一个函数(i, item),其中 i 是指示索引的整数,而 item 是实际对象。这就是文档显示它正在完成的方式 - 该方法被描述为函数回调(indexInArray,valueOfElement)。