【问题标题】:Looping through JSON array with jQuery使用 jQuery 循环遍历 JSON 数组
【发布时间】:2013-10-07 00:34:12
【问题描述】:

我正在尝试遍历一个简单的 JSON 数组并使用 jQuery 显示内容。我的 JSON 数据是:

 {
   "cards":[
      {
         "title":"cat",
         "spanishWord":"gato"
      },
      {
         "title":"dog",
         "spanishWord":"perro"
      }
   ]
}

这是我正在使用的 jQuery:

    var jqxhr = $.getJSON("http://www.myurl.com/cards.js", function (data) {

        $.each(data.cards, function (i, item) {
            $(".list").append("<li id='" + cards[i].title + "'>" + cards[i].title + cards[i].spanishWord + "</li>");
        });
    });

我很确定问题出在我的每个陈述中,但我不知道哪里出了问题。

【问题讨论】:

  • item.title 会更干净

标签: jquery json loops


【解决方案1】:

问题出在循环内部,您使用cards[i] 而不是data.cards[i]

您也可以使用item 代替data.cards[i]

【讨论】:

  • 完美,谢谢!到时我会接受你的回答。
  • 请务必使用item 而不是data.cards[i]。干净多了!
猜你喜欢
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多