【问题标题】:jQuery inserting JSON value to element with specific classjQuery将JSON值插入具有特定类的元素
【发布时间】:2013-02-20 15:39:50
【问题描述】:

我今天尝试整理的一些 jQuery 有点麻烦。

基本上,我想要实现的是通过读取 JSON 提要动态地将价格插入到我页面上的价格按钮中。

这个想法是有一个包含价格的空跨度。我已经给出了所有的价格跨越类.getPriceNew。每个跨度也有一个 id,它等于像 <span class="getPriceNew" id="123456"></span> 这样的项目的 SKU 编号。

机制是,对于每个具有 .getPriceNew 类的跨度,将查询 JSON 以获取信息,并将 SKU id 用作查询字符串的一部分。

这是我尝试过的代码示例

jQuery

$(".getPriceNew").each(function() {
    var sku = $(this).attr("id");
    var priceNew = $.getJSON('/api/MyApi.svc/GetProductBySku/'+sku , function(data) {
            return(data.Variants[0].Price);
            });
    $(this).html("€"+priceNew);        
})

HTML

<span class="getPriceNew" id="123456"></span>
<span class="getPriceNew" id="789123"></span>
<span class="getPriceNew" id="456789"></span>
<span class="getPriceNew" id="654321"></span> 

JSON 示例 这是来自 JSON 提要的单个项目的外观 - 我已经对其进行了一些过滤 /api/MyApi.svc/GetProductBySku/123456

使用有效的 JSON 更新

 {
    "Age": {
        "Description": "Age 18+",
        "Thumbnail": "http://someurl.com/productImages/assets/img//icon18.gif"
    },
    "Barcode": {
        "Barcode": "5026555408684"
    },
    "Description": "HTML",
    "Id": 12214,
    "Packshots": [
        "http://someurl.com/productImages/914383/1min.jpg",
        "http://someurl.com/productImages/914383/2med.jpg",
        "http://someurl.com/productImages/914383/3max.jpg"
    ],
    "Pegis": [],
    "Platform": {
        "Button": "Format",
        "ID": 0
    },
    "Publisher": {
        "Description": null
    },
    "Release": "/Date(1364252400000+0100)/",
    "Screenshots": [],
    "Title": "Product Title",
    "Variants": [
        {
            "Id": 22488,
            "MaxOrderQuantity": 3,
            "Presellable": true,
            "Price": 49.97,
            "Sku": 914383,
            "Type": {
                "Description": "Pre-Order"
            }
        }
    ],
    "Vendor": {
        "Description": "Take Two Interactive Software"
    },
    "VideoHTML": "HTML",
    "status": {
        "Response": "product found",
        "Success": true
    }
}

我很想在这方面得到一些帮助,因为在这个阶段我真的很摸不着头脑。我已经设法让 console.log 将价格输出到日志中,但是当我尝试将它们重新插入跨度时,我得到的只是 [object] [Object]。

【问题讨论】:

标签: javascript jquery json getjson


【解决方案1】:

你在做

$(".getPriceNew").each(function() {
    var sku = $(this).attr("id");
    var priceNew = $.getJSON('/api/MyApi.svc/GetProductBySku/'+sku , function(data) {
            return(data.Variants[0].Price);
            });
    $(this).html("&euro;"+priceNew);        
})

getJSON 返回一个jqXHR 对象,这不是您需要的。试试这个:

$(".getPriceNew").each(function() {
    var sku = $(this).attr("id");
    // Save a refference to this span.
    var thisSpan = $(this);
    $.getJSON('/api/MyApi.svc/GetProductBySku/'+sku , function(data) {
            // Request complete, NOW we can use the data we got!
            thisSpan.html("&euro;"+data.Variants[0].Price); 
    });

})

回调是您想要使用从 AJAX 调用中获得的数据的地方。所有 AJAX 方法($.ajax、$.get、$.post、$.getJSON 等)都将返回一个 jqXHR 对象。

【讨论】:

  • 是的,jQuery.getJSON 返回一个 jqXHR(其中 -- 旁注 -- 是一个 Deferred 实现)。所以+1:更新需要在回调中发生。
  • 好的,谢谢您的提示。似乎我的$(this).html("&amp;euro;"+data.Variants[0].Price); 现在有点麻烦它似乎没有为每个.getPriceNew 跨度设置HTML。 'this' 是否可能不再是那个 jquery 对象?
  • 确实,我刚刚检查过,现在$(this) 是 JSON,不再是 span 选择器。关于如何构建它以便我仍然可以插入我的价格的任何提示?
  • @Jeff 这是我想出的 - 到目前为止似乎有效$(".getPriceNew").each(function() { var sku = $(this).attr("id"); $.getJSON('/api/AthenaService.svc/GetProductBySku/'+sku , function(data) { // Request complete, NOW we can use the data we got! $(".getPriceNew:first").html("&amp;euro;"+data.Variants[0].Price).attr("class", ""); }); });
【解决方案2】:

我认为您的 javascript 代码是正确的,但您的 Json 输出有两个错误: 1:“描述”:“这里有一些 HTML 描述,

所以你的 Json 会变成这样:

{
"Age": {
    "Description": "Age 18+",
    "Thumbnail": "http://url.com/image.gif"
},
"Barcode": {
    "Barcode": "4876416541647"
},
"Description": "Some HTML Description here",
"Id": 12214,
"Packshots": [
    "http: //url.com/productImages/914383/1min.jpg",
    "http: //http: //url.com/2med.jpg",
    "http: //http: //url.com/3max.jpg"
],
"ID": 0,
"Publisher": {
    "Description": null
},
"Release": "/Date(1364252400000+0100)/",
"Screenshots": [],
"Title": "Titleoftheproduct",
"Variants": [
    {
        "Id": 22488,
        "MaxOrderQuantity": 3,
        "Presellable": true,
        "Price": 49.97,
        "Sku": 914383,
        "Type": {
            "Description": "Pre-Order"
        }
    }
],
"Vendor": {
    "Description": "Vendorname"
},
"VideoHTML": "&lt;iframewidth=&quot;725&quot;height=&quot;408&quot;src=&quot;http: //www.youtube.com/embed/videoseries?list=PLofwA47XDv2_KnfUY52_8mlWg0iUEv8ci&quot;frameborder=&quot;0&quot;allowfullscreen&gt;&lt;/iframe&gt;",
"status": {
    "Response": "productfound",
    "Success": true
}  }

现在你的代码

data.Variants[0].Price

将返回 49.97 要验证 Json,您可以将其粘贴到 http://jsonlint.com/ 我认为非常有用

希望对你有帮助

【讨论】:

  • 感谢您的建议。我很确定当我将 JSON 粘贴到这里时,我只是将其处理掉了。我刚刚通过 JSONlint 运行了实时 JSON,它得到了验证
  • 我已更新 JSON 示例以获得有效代码。感谢您指出错误。
猜你喜欢
  • 2015-11-08
  • 2021-07-18
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多