如果我返回匹配的 2 个价格,我将如何向 jquery 中的元素添加一个类?
假设这两个项目(Price1 和 Price2)都通过 JSON 返回,如果它们匹配,则将 class="red" 添加到元素。
下面是我现有的代码,我需要匹配res.amzprice和res.lowprice,如果两者都匹配我需要添加一个类到 res.lowprice td :)
$.ajax({ type: "POST",
url: "aWeb.asmx/GetLowPrices",
data: "{ }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#loading').hide();
var result = response.d;
var new_record = "";
$.each(result, function (index, res) {
new_record = "<tr id=\"" + res.sku + "\">" +
"<td scope=\"row\" class=\"t-left\"><a href=\"../product/editproduct.aspx?sku=" + res.sku + "\" title=\"Edit this product listing\">" + res.title + "</a></td>" +
"<td>" + res.sku + "</td>" +
"<td> £" + res.tprice + "</td>" +
"<td class=\"amzprice-edit\"><input type=\"text\" id=\"txt" + res.sku.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-') + "\" value=\"" + res.amzprice + "\"> <img src=\"images/icons/info.png\" alt=\"Info\" title=\"Price (inc. Shipping): £xx.xx\" /></td>" +
"<td> £" + res.lowprice + " <img src=\"images/icons/info.png\" alt=\"Info\" title=\"Price: £xx.xx | Shipping: £xx.xx\" /> <a href=\"lowpricedata.aspx?sku=" + res.sku + "\" id=\"lnkInfoGrpah\" class=\"link-to info-graph\" data-fancybox-type=\"iframe\"><img src=\"images/icons/graph-icon.png\" alt=\"Info\" title=\"View Lowprice History\" /></a></td>" +
"<td><div class=\"twe-button mini update\">Update</div></td>" +
"</tr>";
$('#dataResults').append(new_record);
});
},
error: function (msg) {
$('#loading').hide();
$('#dataTable').hide();
$('#infoMsg').show();
$('#infoMsg').addClass('er-red');
$('#infoMsg').html("Error while calling web service.");
}
});