Jquery UI slider with two handles with input from two text box?

前端 未结 2 1135
生来不讨喜
生来不讨喜 2020-12-16 23:42

I need a jquery slider with two handles with inputs from two textbox like this http://www.israel-diamonds.com/search/diamonds/default.aspx .Currently i have a single handle

相关标签:
2条回答
  • 2020-12-16 23:51

    From the official jQuery UI Documentation: https://jqueryui.com/slider/#range

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>jQuery UI Slider - Range slider</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
    </head>
    <body>
    
        <p>
            <label for="amount">Price range:</label>
            <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
        </p>
        <div id="slider-range"></div>
    
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.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>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-16 23:59

    Assuming you have two <input> elements:

    <input type="text" class="sliderValue" data-index="0" value="10" />
    <input type="text" class="sliderValue" data-index="1" value="90" />
    

    And a slider placeholder element:

    <div id="slider"></div>
    

    You can use the values option to put the slider widget in multiple handle mode, and synchronize the values of the <input> elements with:

    $("#slider").slider({
        min: 0,
        max: 100,
        step: 1,
        values: [10, 90],
        slide: function(event, ui) {
            for (var i = 0; i < ui.values.length; ++i) {
                $("input.sliderValue[data-index=" + i + "]").val(ui.values[i]);
            }
        }
    });
    
    $("input.sliderValue").change(function() {
        var $this = $(this);
        $("#slider").slider("values", $this.data("index"), $this.val());
    });
    

    You can see the results in this fiddle.

    0 讨论(0)
提交回复
热议问题