过滤器编号范围功能问题
Filter Number range Function Issue
已解决
在之前的一篇文章中,我写了一个函数,在一些建议的帮助下,从技术上讲,它应该做我要求做的事情。然而,结果并不完全正确。
我试图在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
相关文章:
- 较大引导转盘中的较小引导转盘-较小引导转盘上的旋转功能问题
- jQuery Div滚动功能:IE中的问题
- 用户使用浏览器时SPA中的问题's的后退或刷新功能
- 图像上传功能将++(+1)副本添加到下一次上传.出了什么问题或如何重置“选定图像”
- 使用具有同步和启动功能的弹性滑块时出现问题
- 剑道UI移动版 - 按钮点击时调用功能时出现问题
- 从根本上说,函数调用出了问题.功能在控制台中确实有效
- 下拉菜单的jQuery切换功能出现问题
- 功能范围外的图像参考问题
- 引导工具提示/popover功能问题
- jQuery:窗口大小调整功能问题
- ExtJs 4.1网格's分组功能启用和禁用问题
- 将 SVG 转换为图像时画布到 URL 功能出现问题
- 创建可在任何地方使用的 JS 函数?范围和功能“未定义”的问题
- 设置超时功能在 chrome 扩展程序弹出窗口.js问题
- j查询验证器功能覆盖问题
- JSTREE - 重命名功能问题(单击重命名后,节点上的文本输入不接受更改)
- 关于工厂功能概念的问题
- 导出功能的问题
- 我在循环中遇到闭包问题,但我仍然使用单独的功能