简体   繁体   中英

bootstrap-slider link input number with slide change

I am using bootstrap-slider.js . My problem is that I can't link input number with a slide. I want to slide to change too if the input number is changed.

Slider

<div id="slider-year" class="search-block">
  <h2 class="title">Production year</h2>
  <div class="slider-year-amount">
    <span class="pull-left">1900</span>
    <span class="pull-right">2017</span>
  </div>
  <input type="text" class="year-slider" style="width:100%" data-slider-min="1900" data-slider-max="2017" data-slider-step="1" data-slider-value="[1900,2017]" />
  <div class="row">
    <div class="year-box">
      <div class="col-lg-6">
        <label>From</label>
        <input type="number" min="1900" max="2019" class="form-control" placeholder="1900" id="minYear" value="1900" name="year_since">
      </div>
      <div class="col-lg-6">
        <label>To</label>
        <input type="number" min="1901" max="2020" class="form-control" placeholder="2017" id="maxYear" value="2017" name="year_to">
      </div>
    </div>
  </div>
</div>

How I try to update slider:

jQuery("#minYear").change(function () {
    jQuery("#slider-year").slider('setValue', jQuery(this).val());
});

Also here is documentation for this slider if anyone is curious, I couldn't find solution there: http://www.eyecon.ro/bootstrap-slider

JsFiddle: https://jsfiddle.net/y95vfds3/6/

Many years have passed... Anyway, here is my solution:

$('#year-slider').slider()
  .on('slide', function(ev) {
    $('#minYear').val(ev.value[0]);
    $('#maxYear').val(ev.value[1]);
  });

$('.form-control').on('input', function() {
  let minVal = parseInt($('#minYear').val());
  let maxVal = parseInt($('#maxYear').val());
  $('#year-slider').slider('setValue', [minVal, maxVal])
});

https://jsfiddle.net/p98bcxuv/

Do you want like this?

    $("#minYear").change(function() {
      $("#slider-year").slider('setValue', jQuery(this).val());
    });

    val = $('.year-slider').slider();

    $('.year-slider').on('slide', function(ev) {
      $('#minYear').val(ev.value[0]);
      $('#maxYear').val(ev.value[1]);
    });

https://jsfiddle.net/gnanavelr/y95vfds3/7/

https://jsfiddle.net/gnanavelr/y95vfds3/8/

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