0

如何通过 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
4

1 回答 1

1

未经测试,但应该做的伎俩。把它扔到你的成功函数中。

$.each($(data), function(i, obj){
   console.log(i); //spits out your index into console
});
于 2012-06-15T19:10:36.000 回答