简体   繁体   中英

Take slider value from jQuery ui slider and store it into a variable

I am trying to take the values from this slider:

  $(function () {
     $("#slider-range").slider({
        range: true,
        min: 2000,
        max: 8500,
        values: [2000, 8000],
        step: 200,
        slide: function (event, ui) {
            $("#dl-sv-input-mmr").val(ui.values[0] + " - " + ui.values[1]);

        }
     });
      $("#dl-sv-input-mmr").val($("#slider-range").slider("values", 0) +
        " - " + $("#slider-range").slider("values", 1));
   });

How can I take it's current value and store it into a variable? I am trying to do a filter in AJAX that will get all elements in a certain range. I am a newb and not sure if this is the correct way to do it.

HTML:

  <label for="dl-sv-input-mmr" class="control-label col-xs-2">MMR:</label>

        <div class="col-xs-4">
            <input type="text" class="form-control" id="dl-sv-input-mmr" name="dl-sv-input-mmr" readonly
                   style="border:0; color:black; font-weight:bold;">

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

Please take note that you need to have this in your html in order for the code to work

<div id="slider-range"></div>
<input id="dl-sv-input-mmr" type="text"/>

Define this two global variables

var min = 0; 
var max = 0; 

$(function () {
    $("#slider-range").slider({
        range: true,
        min: 2000,
        max: 8500,
        values: [2000, 8000],
        step: 200,
        slide: function (event, ui) {
              $("#dl-sv-input-mmr").val(ui.values[0] + " - " + ui.values[1]);
              min = ui.values[0]; //current min slider value
              console.log("min "+min);
              //You can also do min = $("#slider-range").slider("values", 0);
              max = ui.values[1]; //current max slider value 
              console.log("max "+max);
              //You can also do max = $("#slider-range").slider("values", 1);
        }
     });
    $("#dl-sv-input-mmr").val($("#slider-range").slider("values", 0) +
                          " - " + $("#slider-range").slider("values", 1));
    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);
}); 

Also note that you have to close $(function() {

Assigning max and min values inside your slide handler will ensure that everytime you will have the updated values.

At any point you can do var min = $("#slider-range").slider("values", 0); var max = $("#slider-range").slider("values", 1);

Check http://api.jqueryui.com/slider/#method-values

Check also if you are loading jQuery js file and jQuery-ui js and css

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM