我试图根据数据库中的信息更改元素列表的类。我认为最简单的方法是通过数组。我在php端构建数组,如下所示。

 $setClassResult = array();
 while($row = mysql_fetch_array( $result ))
 {
 $setClassResult= array_push_assoc($setClassResult, $row['item_id'], $row['parent']);
 }
 echo json_encode(array($setClassResult));
 break;


给我...

 [{"830":"0","734":"830","733":"830","732":"830","735":"830","737":"830","736":"830","738":"830","739":"830","740":"830","741":"830","742":"830","872":"0","869":"872","868":"872","880":"872","964":"872"}]


解码和更改我使用的元素.....

  $.each(data, function(key, val) {
        $("#recordsArray_"+key).toggleClass(val);
        alert(key+" "+val);
  });


该警报使用0[object,Object]发生一次,这是由于创建数组的方式吗?我注意到的第一件事是JSON周围的[]

最佳答案

无需添加额外的数组,请尝试:

echo json_encode($setClassResult);

10-04 12:03