过滤器编号范围功能问题

Filter Number range Function Issue

本文关键字:问题 功能 范围 编号 过滤器      更新时间:2023-09-26

已解决

在之前的一篇文章中,我写了一个函数,在一些建议的帮助下,从技术上讲,它应该做我要求做的事情。然而,结果并不完全正确。

我试图在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,并且只在价格在给定价格范围内时显示这些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
    }); 
}

这可能与小数点有关吗?

尝试在数字变量上使用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