如何通过jQuery迭代JSON返回并返回每个对象的索引?
我的一些代码示例如下:
foreach ( x = somevalue ; x < length of array ; x +++
{
that x must be the index
[0] => Array
$('#actual_'+x).text(data.actual+" hrs.");
$('#total_'+x).text(data.total+" hrs.");
$('#regular_'+x).text(data.regular+" hrs.");
}
(
[0] => Array
(
[actual] => 9
[total] => 10
[regular] => 0
[over] => 11
[total_h] => 11
[eng_pay] => 148.5
[rate] => 9
)
[1] => Array
(
[actual] => -1
[total] => 0
[regular] => -1
[over] => 2
[total_h] => 1
[eng_pay] => 18
[rate] => 9
)
)
我想遍历这个jQuery ajax小部件的成功函数:
$.ajax({
type:'POST',
url: 'cal_grid.php',
dataType: 'json',
cache: false,
data:$('#grid_frm').serialize(),
success: function(data)
{
alert(data);
} // response call back ends
});//ajax call ends
答案 0 :(得分:1)
$.each($(data), function(i, obj){
console.log(i); //spits out your index into console
});