使用jQuery在列表内循环 [英] looping within list using jQuery

查看:108
本文介绍了使用jQuery在列表内循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过帮助我的最后一个问题解决了,但仍然卡住了.

By help my last question got solved but still stuck..

尝试此操作后,控制台出现错误日志.

After trying this I got an error on console log..

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
  }
]  

我想遍历列表.任何想法..

I want to loop through the list.. Any ideas..

编辑在@Vucko的建议之后,我尝试用JSON数组本身替换data.d,但令我惊讶的是

EDIT After @Vucko's suggestion I tried replacing data.d by the the JSON array itself and to my surprise it worked JSFiddle.. Any reason and solution for it..

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

Solved changed var list = { "Persons": $.parseJSON(data.d)};.. JSON response is not enough I guess.. We need to parse it also..

推荐答案

您的代码只有一个错误:

Your code has only one error :

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

更改为:

var list = { "Persons": data };

JSFiddle

这篇关于使用jQuery在列表内循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆