使用jQuery在列表中循环

时间:2013-07-19 21:08:16

标签: jquery list

帮助my last question已经解决但仍然卡住了..

After trying this我在控制台日志中出错了。

Error: Syntax error, unrecognized expression: [ { "id": 1, "name": "Bhavik", "phone": 9601109585 }, { "id": 2, "name": "Xyz", "phone": 1234567890 }, { "id": 3, "name": "Abc", "phone": 9876543210 } ]
[Break On This Error]   

throw new Error( "Syntax error, unrecognized expression: " + msg );  

jQuery代码:

var list = { "Persons": data.d };
$(list.Persons).each(function (index) 
{
     alert( this.id + "\n" + this.name + "\n" + this.phone);
});

JSON数组:

[
  {
    "id": 1,
    "name": "Bhavik",
    "phone": 9601109585
  },
  {
    "id": 2,
    "name": "Xyz",
    "phone": 1234567890
  },
  {
    "id": 3,
    "name": "Abc",
    "phone": 9876543210
  }
]  

我想循环浏览列表..任何想法..

编辑在@Vucko的建议之后,我尝试用JSON数组本身替换data.d,令我惊讶的是它工作JSFiddle ..任何理由和解决方案......

已解决已更改var list = { "Persons": $.parseJSON(data.d)}; .. JSON响应不够我猜...我们还需要解析它..

1 个答案:

答案 0 :(得分:1)

您的代码只有一个错误:

var list = { "Persons": data.d };
                             ^

更改为:

var list = { "Persons": data };

JSFiddle