【问题标题】:Iterate through JSON based on ID根据 ID 遍历 JSON
【发布时间】:2015-09-19 18:33:25
【问题描述】:

我正在处理一个网上商店模板,我需要根据商店中的每一个产品,在单击给定产品时打开的模式中显示正确的 JSON 数据。我还使用车把进行模板化,并具有以下代码结构:

.html(打开模式的按钮)

<button type="button" class="quickview">Open Modal</button>

.html(模态)

<div id="quickview-modal">
 modal content that should iterate over the JSON and display one item at a time depending on the ID 
</div>

.json

{
 "productID" : "1",
 "name" : "productOne"
},
{
 "productID" : "2",
 "name" : "productTwo"
}

.js

$( ".quickview" ).click(function( event ) {
  $('#quickview-modal').modal('show');
  event.stopPropagation();
  // Do something
});

有什么想法可以解决这个问题吗?谢谢!

【问题讨论】:

标签: javascript jquery html json


【解决方案1】:

说你的 .json 变量是data

 var data = 'your JSON';
 $.each(data,function(i,v){
     alert(v.productID); //will give you the value related to productID.
 });

更新

$( ".quickview" ).click(function( event ) {
   event.stopPropagation();
   var data = 'your JSON';
   $.each(data,function(i,v){
      $('#quickview-modal').append(v.name + " : " + v.productID);

   });
   $('#quickview-modal').modal('show');
 });

【讨论】:

  • 我如何找到正确的 .json 变量,它是一个很长的 .json,有几层嵌套的元素?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多