4

我已经创建了我的 jquery 价格滑块,但我不确定如何过滤我的结果,以便在滑动时您只能看到具有该值的产品。

HTML:

<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>
        <li> product - £10 </li>
        <li> product - £50 </li>
        <li> product - £100 </li>
        <li> product - £150 </li>
        <li> product - £200 </li>
    </ul>
</div>​

JavaScript:

$(function() {
    var options = 
        {
            range: true,
            min: 0,
            max: 500,
            values: [ 50, 300 ],
            slide: function( event, ui ) {
                $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
            }
        };

        $( "#slider-range" ).slider(
            options
        );
        $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
            " - $" + $( "#slider-range" ).slider( "values", 1 ) );
    });

​我的小提琴是http://jsfiddle.net/ktcle/uvJ8F/

4

1 回答 1

18

我会做一些改变:

  1. 为您的产品列表赋予一个id属性,并为每个产品赋予一个data-price等于商品价格的属性:

    <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>
    
  2. 添加一个在slide事件发生时显示或隐藏这些产品的函数:

    function showProducts(minPrice, maxPrice) {
        $("#products li").hide().filter(function() {
            var price = parseInt($(this).data("price"), 10);
            return price >= minPrice && price <= maxPrice;
        }).show();
    }
    
  3. slide事件处理程序调用该函数:

    slide: function(event, ui) {
        var min = ui.values[0],
            max = ui.values[1];
    
        $("#amount").val("$" + min + " - $" + max);
        showProducts(min, max);
    }
    
  4. 在滑块初始化后立即调用该函数,以便最初隐藏正确的产品:

    var options = {
       /* snip */
    }, 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);
    

整个片段:

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/

于 2012-05-28T16:25:03.407 回答