解决了
从我之前的一篇文章中,在一些建议的帮助下,我设法编写了一个在技术上应该按照我的要求做的函数。然而结果并不完全正确。
我试图向 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
从使用警报开始一切正常,但是当它到达过滤器的 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
});
}
这可能与小数点有关吗?