jquery - 比较jquery中的2个价格,如果它们匹配添加一个类?

标签 jquery

如果我返回的 2 个价格匹配,我将如何向 jquery 中的元素添加一个类?

假设两个项目(Price1 和 Price2)都通过 JSON 返回,如果它们匹配,则将 class="red"添加到元素。

下面是我现有的代码,我需要匹配 res.amzpriceres.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.");
    }
});

最佳答案

我建议:

$.each(result, function (index, res) {

    var lowPriceClass = res.amzprice == res.lowprice ? 'red' : 'noMatch';

    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 class='" + lowPriceClass + "'> £" + 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);
});

虽然我觉得任何显示 lowPrice 的元素都应该评估“低价”是否小于或等于,而不是简单地等于,所以我真的建议:

    var lowPriceClass = res.amzprice <= res.lowprice ? 'red' : 'noMatch';

此方法使用三元运算符来评估条件以返回 bool 值,如果发现为 true,则返回第一个值/变量(在本例中为 'red' 之后和 ? 之前的 : )(如果找到)如果为 false,则返回第二个值/变量(: 之后的那个值/变量)并将返回的值/变量分配给变量名称。

关于jquery - 比较jquery中的2个价格,如果它们匹配添加一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12162348/

相关文章:

jquery - div 与 jquery animate 滚动不平滑

javascript - 获取一个对象数组并将其放入条形图中

jquery - 如何验证 Twitter Bootstrap 表单向导

javascript - 一次只显示一个下拉项

jquery - 自动完成文本框设计不匹配

javascript - 处理 React.componentDidMount 中的 AJAX 错误

javascript - 在 jQuery 函数中调用类方法

javascript - 对输入类型 ="number"使用正则表达式删除非数字

php - 设置 JQuery 选择不执行任何操作

jquery - 比较两个 img src