【问题标题】:jquery and javascript filtering issuejquery 和 javascript 过滤问题
【发布时间】:2012-05-23 01:17:17
【问题描述】:

我的 JS 代码 here 有两个问题,非常感谢任何帮助...

  1. 我需要添加一个逻辑,以便当产品价格为 499 美元时,过滤列表中会显示“超过 50 个”选项。就我目前的实现而言,这是行不通的。

    <li>
        <!-- over 50 product is not showing when filtering -->
        <a href="/product-three">Product Four</a><br>
        <span id="lowestPriceRange">400</span>
        <span id="highestPriceRange">400</span>
    </li>
    
    
    // Because I need to handle it here somehow but I'm stuck
    var hidePrices = {};
    hidePrices[0] = true;
    hidePrices[10] = true;
    hidePrices[20] = true;
    hidePrices[30] = true;
    hidePrices[40] = true;
    hidePrices[50] = true;
    
    $('#products').find('span').each(function(i, el){
        var key = parseInt(Math.floor($(this).html() / 10) * 10, 10);
        hidePrices[key] = false;
    });
    
    $('#filterByPrice').find('li').each(function(i, el){
        if (hidePrices[Number($(this).find('a').attr('name'))]) {
            $(this).hide();
        }
    });
    
  2. 它隐藏了一些我想显示的产品。例如,我想在选择$ 40- $ 50过滤选项时显示价格在40美元至60美元之间的产品。

【问题讨论】:

  • 在此处粘贴代码的相关部分。如果 jsfiddle 今晚宕机了怎么办?你的问题没有任何意义。
  • 好点。现在将代码示例添加到我的帖子中。

标签: javascript jquery filtering


【解决方案1】:

我在这里解决了你的问题:http://jsfiddle.net/qNFWM/7/

为了解决第一个问题,我添加了这个,因为您的密钥是 400:

if (key > 50) key = 50;

为了解决第二个问题,我改变了这个:

return (minProductPrice >= minSelectedPrice && maxProductPrice <= maxSelectedPrice);

到这里:

return ((minProductPrice >= minSelectedPrice &&  minProductPrice <= maxSelectedPrice) || (maxProductPrice >= minSelectedPrice &&  maxProductPrice <= maxSelectedPrice));

所以只有最小值或最大值必须在范围内。

【讨论】:

  • 如果您想添加更多范围,我们可以为我的第一个修复制作一个更动态的版本。
  • 这对现在来说是完美的......我非常感谢你!我仍在学习 Javascript,非常感谢您的帮助。
猜你喜欢
  • 2015-08-04
  • 2013-06-15
  • 2013-05-13
  • 1970-01-01
  • 2021-11-15
  • 2013-05-30
  • 2018-05-01
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多