在 jQuery 中使用数组

Working with arrays in jQuery

本文关键字:数组 jQuery      更新时间:2023-09-26

我被困在jQuery中的一个数组上。我从 php 返回一个数组 在 php 文件中,我在循环中添加这样的数组:

$table_data[]= array("id"=>mysql_result($query,$i,"id"),"name"=>trim(mysql_result($query,$i,"name")));

在 PHP 文件的末尾:

echo json_encode($table_data);

在我的jquery上:

 $.ajax({
   type: "POST",
   url: "phpfilename.php",
   data: ({
     newtask: "grab"
   }),
   dataType: "json",
   success: function(data){
     alert("value - "+data.length);
   }
 });

(这将返回正确的记录计数(

$.each(data, function(key, value) {
 alert( "The key is '" + key + "' and the value is '" + value + "'" );
});

(上面的循环返回我:0,1,2作为键,对象作为值(

我需要 understanidn 的帮助,如何将数组从 php 传递到 jquery 以及如何处理它们。也许我完全走错了路。

要访问对象的任何属性,过程是:

ObjectName.property 

ObjectName[''+ property +''];

在这里阅读

$.each(data, function(key, value) {
  // try
  console.log(value.id);
  console.log(value.name);
});