jQuery的$。每个回路和JSON数据 [英] jQuery $.each loop and json data

查看:93
本文介绍了jQuery的$。每个回路和JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法得到这个工作,我检索了一些JSON数据,并通过它只是试图循环它追加到DIV。这里是我的code:

如何JSON返回(PHP)

<$p$p><$c$c>{"list":[{"subscribe":"example"},{"subscribe":"example2"},{"subscribe":"example3"},{"subscribe":"example4"},{"subscribe":"example5"}]}

我的JSON CALL使用jQuery

  $。的getJSON(article.php功能(数据)
   $每个(Data.List模块,功能(我的数据){
      VAR的ListData =&LT;李&gt;中+ data.subscribe +&LT; /李&gt;中;
      $('#列表)追加(的ListData)。
   });
});
 

我不获取任何错误控制台,但什么也没发生,我知道调用成功,我是在做正确的。每次循环?任何人都可以请帮助?

解决方案

也许你可以试试这个:

  $。每个(Data.List模块,功能(I,项目){
    VAR的ListData =&LT;李&gt;中+ item.subscribe +&LT; /李&gt;中;
    $('#列表)追加(的ListData)。
});
 

只要修改功能(我的数据)函数(一,项目)

通过重用变量名数据的方式,你是,你是不是真正遍历数组,你期望。

I can't seem to get this working, i am retrieving some json data, and just trying to loop through it an append it to a DIV. here is my code:

HOW THE JSON IS RETURNED (PHP)

{"list":[{"subscribe":"example"},{"subscribe":"example2"},{"subscribe":"example3"},{"subscribe":"example4"},{"subscribe":"example5"}]}

MY JSON CALL USING JQUERY

$.getJSON("article.php",function(data)
   $.each(data.list, function(i,data) {
      var listData = "<li>" + data.subscribe + "</li>";
      $('#lists').append(listData);
   });
});

I am not retrieving any errors in the console, but nothing is happening, i know the call is successful, am i doing the .each loop correctly? can anyone please help?

解决方案

Maybe you can try this:

$.each(data.list, function(i, item) {
    var listData = "<li>" + item.subscribe + "</li>";
    $('#lists').append(listData);
});

Just change function(i, data) to function(i, item).

By reusing the variable name data in the way that you are, you are not actually looping through the array as you expect to.

这篇关于jQuery的$。每个回路和JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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