简体   繁体   English

使用多个滑块更新输入值

[英]Update input values multiple sliders

I've been using nouislider https://refreshless.com/nouislider/ . 我一直在使用nouislider https://refreshless.com/nouislider/ The slider must handle with two values. 滑块必须处理两个值。 This one works but i need it multiple times (about 10 sliders). 这个工作,但我需要它多次(约10个滑块)。 Each slider must have his own 2 inputs (like the fiddle below) 每个滑块必须具有自己的2个输入(例如下面的小提琴)

How can i use this for more than one slider: 如何将其用于多个滑块:

var slider = document.getElementById('slider');

noUiSlider.create(slider, {
            start: [1, 10000000],
      range: {
        'min': 1,
        'max': 1000000
      },
      connect: true,
      step: 1
});

var valueInputMin = document.getElementById('valueInputMin');
var valueInputMax= document.getElementById('valueInputMax');
slider.noUiSlider.on('update', function( values, handle ) {
    if ( handle ) {
        valueInputMax.value = values[handle];
    } else {
        valueInputMin.value = values[handle];
    }
});

valueInputMin.addEventListener('change', function(){
    slider.noUiSlider.set([this.value,null]);
});
valueInputMax.addEventListener('change', function(){
    slider.noUiSlider.set([null, this.value]);
});

fiddle: https://jsfiddle.net/cprixi/haqjk8vb/6/ 小提琴: https : //jsfiddle.net/cprixi/haqjk8vb/6/

You can use following code with your change if required 您可以根据需要使用以下代码进行更改

var sliders= [];
var valueInputMin = [];
var valueInputMax=  [];

// change i to as many html element you have
// you can also create and a html in the same loop
for(var i=1; i<=2; i++){
    sliders[i] = document.getElementById('slider'+i);

  noUiSlider.create(sliders[i], {
        start: [0, 100],
        range: {
          'min': 0,
          'max': 100
        },
        connect: true,
        step: 1
  });

  valueInputMin[i] =    document.getElementById('valueInputMin'+i);
  valueInputMax[i] = document.getElementById('valueInputMax'+i);

   sliders[i].noUiSlider.on('update', function(values, handle)        {
   var id = this.target.id;
   var index = id.charAt(id.length - 1);
    if ( handle ) {
      valueInputMax[index].value = values[handle];

    } else {
      valueInputMin[index].value = values[handle];
    } 
});

 valueInputMin[i].addEventListener('change', function(event){
  // index can be found with better other ways
   var id = this.target.id;
   var index = id.charAt(id.length - 1);
   sliders[index].noUiSlider.set([this.value,null]); 
    });


  valueInputMax[i].addEventListener('change', function(event){
   // index can be found with better other ways
    var id = this.target.id;
    var index = id.charAt(id.length - 1);
     sliders[index].noUiSlider.set([null, this.value]); 
  });
}

Core Concept to this is to use arrays to hold the objects.You can also create html elements and append it to the dom in the same loop just to avoid statically defining html before hand.Then you just need to change max value of i in loop to get as many sliders you want. 核心概念是使用数组来保存对象。您也可以创建html元素并将其附加到同一循环中的dom上,以避免事先静态定义html。然后您只需更改in的i最大值获得所需的多个滑块。

Fiddle: https://jsfiddle.net/sksethi25/f3qs08db/6/ 小提琴: https : //jsfiddle.net/sksethi25/f3qs08db/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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