我有一个ajax这样返回json数组数据,
[{
"UserName": "John",
"Total": "45",
"Correct(%)": "71.1111",
"Incorrect(%)": "28.8889"
}, {
"UserName": "KKK",
"Total": "42",
"Correct(%)": "47.6190",
"Incorrect(%)": "52.3810"
}, {
"UserName": "AAA",
"Total": "54",
"Correct(%)": "81.4815",
"Incorrect(%)": "18.5185"
}, {
"UserName": "BBB",
"Total": "39",
"Correct(%)": "58.9744",
"Incorrect(%)": "41.0256"
}]
我想使用这样的javascript显示数据,
用户名:John总计:45正确(%):71.1111
不正确(%):28.8889
用户名:KKK总计:42正确(%):47.6190
不正确(%):52.3810
用户名:AAA总计:54正确(%):81.4815
不正确(%):18.5185
用户名:BBB总计:39正确(%):58.9744
不正确(%):41.0256
所以,我这样尝试
$.ajax({
url: 'some.php',
type: 'post',
data: {dept:d},
dataType: 'json',
success: function(data) {
console.log("success");
var temp = "";
if(data && data!="") {
for(var i=0; i<data.length; i++) {
$.each(data,function(k,v){
$.each(v,function(k,s){
temp +=k+': <b>'+s+'</b>       ';
});
temp +="<br/><br/>";
console.log(temp);
});
}
document.getElementById('user').innerHTML= temp;
}
});
但是,我为每个用户分配了五行。循环时我错了。那么,我该怎么做呢?
最佳答案
尝试删除for
循环。
//for(var i=0; i<data.length; i++) {
$.each(data,function(k,v){
$.each(v,function(k,s){
temp +=k+': <b>'+s+'</b>       ';
});
temp +="<br/><br/>";
console.log(temp);
});
//}