在每个函数中使用jQuery查询JSON数据 [英] Querying JSON data with jQuery in 'each' function

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

问题描述

我是JSON的新手,正试图找到一种查询JSON数据并将信息放入页面上适当的<div>的方法.我使用的是Volusion,因此对代码的访问受到限制(它大部分是从服务器端脚本动态生成的),因此我必须执行一些解决方法.基本上有一个产品表,获取产品代码的唯一方法是从这些产品的定位标记上生成的URL中获取它(每个产品都设置有附加到/的链接). ProductDetails.asp?ProductCode =香气产品的香气).我已经编写了Javascript来提取产品代码(从URL末尾开始)并将其设置为名为productCode的变量.

I'm new to JSON and am trying to find a way to query JSON data and bring the info into an appropriate <div> on my page. I'm using Volusion so the access to the code is limited (it's mostly dynamically generated from server side script) so I'm having to do a couple workarounds. Basically there is a table of products and the only way to get the product code is to grab it out of the URL that's being generated on the anchor tags for those products (each product is set up with a link attached to, for example, /ProductDetails.asp?ProductCode=aroma for the aroma product). I've written Javascript to extract the product code (from the end of the URL) and set it to a variable named productCode.

在我的JSON数据中,我将其设置如下:

In my JSON data, I have it set up like this:

[
    {
        "item": "aroma",
        "title": "Aromatherapy Oils",
        "price": "$10.00",
    },

    {
        "item": "ah-chairbamboo",
        "title": "Modern Bamboo Chair",
        "price": "$100.00",
    },
    {
        "item": "hc-mirror",
        "title": "Cordless LED Lighting Pivoting Vanity Mirror",
        "price": "$45.00",
    },
    {
        "item": "macfrc",
        "title": "French Macarons",
        "price": "$15.00",
    }
]

如果我只想从JSON工作表中获得标题(其中"aroma"是项并将其附加到div中的产品容器)怎么办?我的代码现在获取产品代码的方式是:

What if I wanted to just get the title from the JSON sheet where 'aroma' is the item and append that to product container in a div? The way my code is now to get the product code is:

$(".v65-productDisplay a").each(function(){
    var productHref = $(this).attr("href");
    var productCode = productHref.substring(19,productHref.length);

这行得通,因为我已经提醒了productCode,它们都是正确的.

This works because I have alerted out the productCodes and they are all correct.

我希望此JSON查询明显位于每个函数之内,以便它在页面上的每个产品上运行.用英语,我想说项目值与JSON工作表中的产品代码匹配的地方,我要打印出标题和价格"(是的,我知道这不是很好的英语……).

I'm wanting this JSON query to obviously be within the each function so it runs on each product on the page. In English, I want to say "where the item value matches the product code in the JSON sheet, I want to print out that title and price" (yes, I understand that's not great English...).

我在查询它(并警告该值)时第一次失败的尝试是在这里.在此范围内:

My first failed attempt at querying it (and alerting out the value) is here. This is within:

$(".v65-productDisplay a").each(function(){
var productHref = $(this).attr("href");
var productCode = productHref.substring(19,productHref.length);

$.getJSON("/v/vspfiles/data/productinfo.js", function (data) {
            $.each(data, function () {
                  if (productCode = this['item'])
                     alert(this['price']); 
            });
        });

});

那没有用.抱歉,如果这令人困惑,我正在尝试自己解开它.非常感谢您的帮助.

That did not work though. Sorry if this is confusing, I'm trying to untangle it myself. Thanks so much for any help.

编辑

下面是PSCoder的回答之后,这是一个新的更新的代码段,仍然无法正常工作:

Here's a new updated snippet following PSCoder's answer below that's still not working exactly:

$.getJSON("/v/vspfiles/data/productinfo.js", function (data) {
        $.each(data, function (i,o) {
              if (productCode == o.item)
                 alert(o.price); 
        });
    });

推荐答案

您可以这样做:

$.getJSON("/v/vspfiles/data/productinfo.js", function (data) {
            $.each(data, function () {
                  if (productCode.trim() === this.item)
                     console.log(this['price']); 
            });
        });

这篇关于在每个函数中使用jQuery查询JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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