简体   繁体   English

根据 ID 遍历 JSON

[英]Iterate through JSON based on ID

I am working on a webshop template and I need to display the correct JSON data according to every single product in the store in a modal that opens when clicking the given product.我正在开发一个网上商店模板,我需要根据商店中的每个产品在单击给定产品时打开的模式中显示正确的 JSON 数据。 I am also using handlebars for templating and have the following code structure:我还使用把手进行模板化,并具有以下代码结构:

.html (button to open the modal) .html (打开模态的按钮)

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

.html (modal) .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 .json

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

.js .js

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

Any ideas how I can tackle this?有什么想法可以解决这个问题吗? Thanks!谢谢!

say your .json variable is data说你的 .json 变量是data

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

updated更新

$( ".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');
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM