如何重置jQuery UI滑块?

时间:2012-02-17 16:15:53

标签: javascript jquery jquery-ui-slider

    <!-- dirty -->
    <head>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

    <script>
    $(function() {
        $( "#slider-range" ).slider({
            range: true,
            min: 0,
            max: 500,
            values: [ 75, 300 ],
            slide: function( event, ui ) {
                $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
            }
        });
        $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
            " - $" + $( "#slider-range" ).slider( "values", 1 ) );
    });
    </script>

    </head>

    <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>

    </div>

    <button type="button" 
onclick='$("#slider-range").slider("value", $("#slider-range").slider("option", "min") );'>
Test
</button> 

单击该按钮不会重置滑块。有线索吗?

5 个答案:

答案 0 :(得分:17)

不存储原始选项/多个滑块重置

如果您不想存储原始值或有多个滑块,请尝试以下操作:

// Reset the sliders to their original min/max values
$('.slider').each(function(){

  var options = $(this).slider( 'option' );

  $(this).slider( 'values', [ options.min, options.max ] );

});

关于您的代码,请参阅this demo

答案 1 :(得分:14)

对于范围滑块,您需要使用方法.slider("values", index, value)

<script>
function resetSlider() {
  var $slider = $("#slider-range");
  $slider.slider("values", 0, initialMinimumValue);
  $slider.slider("values", 1, initialMaximumValue);
}
</script>
<button type="button" onclick='resetSlider();'>Test</button> 

答案 2 :(得分:5)

jsfiddle

查看我的答案
$(function() {
    var options = 
        {
            range: true,
            min: 0,
            max: 500,
            values: [ 75, 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 ) );

    $("#button").click(function()
       {
           $("#slider-range").slider("values", 0, options.values[0]);  
           $("#slider-range").slider("values", 1, options.values[1] ); 
           $( "#amount" ).val( "$" + options.values[0] + " - $" + options.values[1] );           
       });
    });

答案 3 :(得分:2)

您可以尝试

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

}  
只要你需要滑块并通过调用

重置,就可以调用函数PrimarySlider();
function ResetPrimarySlider() {
   $( "#slider-range" ).slider("destroy");
   PrimarySlider();
}

答案 4 :(得分:1)

您也可以重置一行代码(如果您使用滑块中的范围选项)

$("#slider-range").slider("option", "values", [some_min_val, some_max_val]);