jQuery滑块忽略逗号和点

时间:2014-07-29 08:10:17

标签: javascript jquery uislider

所以我正在开发这个电子商务,我正在使用jQuery滑块。 这些产品的价格都有点和逗号。 如何使滑块忽略像点这样的特定字符?所以即1.900,00将被读作1900,00?

这是我的js代码

function showProducts(minPrice, maxPrice) {
    $(".product_price").parent().hide();
    $(".product_price").filter(function () {
        var x = $(this).clone();
        $('span', x).remove();
        var price = parseInt($(x).text(), 10);
        return price >= minPrice && price <= maxPrice;
    }).parent().show();
}

$(function () {

    var maxValue = 0;

    $('div.product_price').each(function (index) {
        var price = $(this).contents().filter(function () {
            return this.nodeType === 3;
        }).text();
        if (parseInt(price, 10) > maxValue) {
            maxValue = parseInt(price, 10);
        }
    });

    var minValue = 0;

    $('div.product_price').each(function (index) {
        var price = $(this).contents().filter(function () {
            return this.nodeType === 3;
        }).text();
        if (parseInt(price, 10) < minValue) {
            minValue = parseInt(price, 10);
        }
    });

    var options = {
        range: true,
        min: minValue,
        max: maxValue,
        values: [minValue, maxValue],
        slide: function (event, ui) {
            var min = ui.values[0],
                max = ui.values[1];

            $("#range_amount").val("kr" + min + " - kr" + max);
            showProducts(min, max);
        }
    }, min, max;

    $("#slider-range").slider(options);

    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);

    $("#range_amount").val("kr" + min + " - kr" + max);

    showProducts(min, max);
});

我设置了jsFiddle

3 个答案:

答案 0 :(得分:1)

使用此代码即可使用。我也在JSFiddle上测试过它。

$(".product_price").filter(function () {
    var x = $(this).clone();
    $('span', x).remove();
//This code is edited
        var priceText = $(x).text().replace(".","");
        var price = parseInt(priceText, 10);
//Editing complete
    return price >= minPrice && price <= maxPrice;
}).parent().show();

干杯! :)

****************************为其他用户添加更新的代码********* **********

function showProducts(minPrice, maxPrice) {
    $(".product_price").parent().hide();
    $(".product_price").filter(function () {
        var x = $(this).clone();
        $('span', x).remove();
        var priceText = $(x).text().replace(".","");
        var price = parseInt(priceText, 10);
        return price >= minPrice && price <= maxPrice;
    }).parent().show();
}

$(function () {

    var maxValue = 0;

    $('div.product_price').each(function (index) {
        var price = $(this).contents().filter(function () {
            return this.nodeType === 3;
        }).text();
        var priceText = price.replace(".","");
        if (parseInt(priceText, 10) > maxValue) {
            maxValue = parseInt(priceText, 10);
        }
    });

    var minValue = 0;

    $('div.product_price').each(function (index) {
        var price = $(this).contents().filter(function () {
            return this.nodeType === 3;
        }).text();
         var priceText = price.replace(".","");
        if (parseInt(priceText, 10) < minValue) {
            minValue = parseInt(priceText, 10);
        }
    });


    var options = {
        range: true,
        min: minValue,
        max: maxValue,
        values: [minValue, maxValue],
        slide: function (event, ui) {
            var min = ui.values[0],
                max = ui.values[1];

            $("#range_amount").val("kr" + min + " - kr" + max);
            showProducts(min, max);
        }
    }, min, max;

    $("#slider-range").slider(options);

    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);

    $("#range_amount").val("kr" + min + " - kr" + max);

    showProducts(min, max);
});

答案 1 :(得分:0)

我不确定,但您可以尝试这个,如果您有查询,那么只需在评论中提问

function showProducts(minPrice, maxPrice) {
        $(".product_price").parent().hide();
        $(".product_price").filter(function () {

            var x = $(this).clone();
            $('span', x).remove();
            var price = parseInt($(x).text().replace('.', '').replace(',',''), 10);
            return price >= minPrice && price <= maxPrice;
        }).parent().show();
    }

答案 2 :(得分:0)

从价格字符串中删除点:

var price = $(this).contents().filter(function () {
    return this.nodeType === 3;
}).text().replace('.','');