过滤器编号范围功能问题

时间:2013-07-22 13:41:28

标签: javascript jquery

解决

从我之前发过的帖子中,在一些建议的帮助下,我设法编写了一个技术上应该做我需要的功能。但结果并不完全正确。

我已经尝试将标记添加到JS来解释发生了什么,但是为了解释我有如下所示的div:

<div class="section-link">
    <div class="price"> £59.99</div>
</div>
<div class="section-link">
    <div class="price"> £259.99</div>
</div>

我试图让该功能隐藏所有这些div,只显示价格在给定价格范围内的那些。

我传递给该函数的数据是:£0.01 - £59.99£60.00 - £159.99£160.00 - £500.00

从使用alert的一切工作正常,但当它到达过滤器的if语句时,它不会过滤它应该如何。

任何帮助表示赞赏

js:

function price(string){ // passing in the strings as £0.01 - £59.99 and £60.00 - £159.99 etc
    $('.section-link').hide(); // hide all section-link div's'
    var range = string.replace(/\u00A3/g, ''); // strip pound sign's from range
    var rangearray = range.split("-"); // split into 2 value arrays
    lowarray = rangearray[0].toString(); // get low value as string
    higharray = rangearray[1].toString(); // get high value as string
    lowvalue = lowarray.replace(/ /g,''); // strip spaces
    highvalue = higharray.replace(/ /g,''); // strip spaces

    alert(lowvalue); // testing low value string (is alerting right) - 0.01
    alert(highvalue); // testing high value string (is alerting right) - 59.99

    $(".price").filter(function(){ //do a filter for all div's with the class of price
        var divprice = $(this).text().replace(/\u00A3/g, ''); // strip pound sign from price value
        var maindivprice = divprice.replace(/ /g,''); // strip spaces from price value
        if (maindivprice >= lowvalue && maindivprice <= highvalue) {
            alert(maindivprice); // alerting to see what prices it is saying are between the range (these are showing all the prices and not only ones between the range)
            $(this).parent().show(); // show this parents div
        } // filter to see if this price is in the price range
    }); 
}

是否可能与小数点有关?

1 个答案:

答案 0 :(得分:1)

尝试在数字变量上使用parseFloat,如果这是一个字符串,那么它会尝试将字符串值与浮点值进行比较

lowvalue = parseFloat(lowarray.replace(/ /g,'')); // strip spaces
highvalue = parseFloat(higharray.replace(/ /g,'')); // strip spaces
var maindivprice = parseFloat(divprice.replace(/ /g,'')); // strip spaces from price value