【问题标题】:jQuery price filter - set slide values from the filter objectjQuery 价格过滤器 - 从过滤器对象设置幻灯片值
【发布时间】:2016-04-01 17:02:57
【问题描述】:

我正在使用 jQuery 幻灯片过滤器来过滤产品的价格。您在脚本中设置过滤值 - 但我更愿意根据我的页面上使用数据属性的最高/最低数字动态设置值。

这可能吗?

过滤对象:

<div class="priceinfo col5" data-price="100">100</div>
<div class="priceinfo col5" data-price="1000">1000</div>

脚本:

function showProducts(minPrice, maxPrice) {
$("#products li").hide().filter(function() {
    var price = parseInt($(this).data("price"), 10);
    return price >= minPrice && price <= maxPrice;
}).show();
}

$(function() {
var options = {
    range: true,
    min: 0,
    max: 500,
    values: [50, 300],
    slide: function(event, ui) {
        var min = ui.values[0],
            max = ui.values[1];

        $("#amount").val("$" + min + " - $" + max);
        showProducts(min, max);
    }
}, min, max;

$("#slider-range").slider(options);

min = $("#slider-range").slider("values", 0);
max = $("#slider-range").slider("values", 1);

$("#amount").val("$" + min + " - $" + max);

showProducts(min, max);
});

http://jsfiddle.net/5aPg7/

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    你可以得到所有price的值,然后找到最大值和最小值

    function showProducts(minPrice, maxPrice) {
      $("#products li").hide().filter(function() {
        var price = parseInt($(this).data("price"), 10);
        return price >= minPrice && price <= maxPrice;
      }).show();
    }
    
    $(function() {
      var prices = $('#products li').map(function() {
        return $(this).data('price');
      }).get();
      var options = {
          range: true,
          min: Math.min.apply(Math, prices),
          max: Math.max.apply(Math, prices),
          values: [50, 300],
          slide: function(event, ui) {
            var min = ui.values[0],
              max = ui.values[1];
    
            $("#amount").val("$" + min + " - $" + max);
            showProducts(min, max);
          }
        },
        min, max;
    
      $("#slider-range").slider(options);
    
      min = $("#slider-range").slider("values", 0);
      max = $("#slider-range").slider("values", 1);
    
      $("#amount").val("$" + min + " - $" + max);
    
      showProducts(min, max);
    });
    .demo {
      padding: 25px;
    }
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
    <div class="demo">
    
      <p>
        <label for="amount">Price range:</label>
        <input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;" />
      </p>
    
      <div id="slider-range"></div>
      <ul id="products">
        <li data-price="10">product - £10</li>
        <li data-price="50">product - £50</li>
        <li data-price="100">product - £100</li>
        <li data-price="150">product - £150</li>
        <li data-price="200">product - £200</li>
      </ul>
    </div>

    【讨论】:

    • 哇,这很有趣。它确实工作得很好。一个后续问题 - 是否可以将值(值:[50, 300],)也设置为最低/最高数字?我基本上是想让滑块在两端一直展开。
    • @JerrySvensson 抱歉,我不确定我是否完全了解您
    • 对不起 - 我试着反思一下。滑块 - 它按定义的值显示(值:[50, 300]),但我希望它按动态值显示(最小值:Math.min.apply(Math,价格),最大值:Math.max.apply(数学,价格),),以便 tje sldiebar 永远不会有任何空格 - 它应该从最低数字开始并以最高数字结束
    • 实际上 - 我刚刚意识到如果我将 0 设置为一个非常高的数字,它会做我想要的。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多