noUiSlider多范围访问第一个句柄

时间:2018-10-16 15:54:42

标签: javascript nouislider

我正在尝试构建具有多个句柄的noUiSlider,在这里我可以获取表单提交的各个句柄之间的值。我可以访问所有句柄的值,但不能访问第一个句柄。试图在console.log上获取其ID并没有给我任何帮助。如何访问第一个手柄? 我的例子在这里: Codepen Example

var multirange = document.getElementById("my-slider");
//initalize the slider
noUiSlider.create(multirange, {
  start: [1, 3, 6, 7],
  connect: [true, true, true, true, true],
  tooltips: [true, true, true, true],
  behaviour: "tap-drag",
  step: 0.25,
  range: {
    min: [0],
    max: [8]
  }
});

multirange.noUiSlider.on("update", function(values, handle) {
  var value = values[handle];

  if (handle) {
    //for debugging check the id of the current handle
    console.log(handle);
    //get the id of the corresponding input field
    var inputid = "input-number" + handle;
    //first handle value does not need to be processed further
    if (handle !== 0) {
      //get previous handle id field
      var prvid = handle - 1;
      //get previous handle value
      var prvvalue = values[prvid];
      //difference between current and previous value
      var diffvalue = value - prvvalue;
      var inputNumber = document.getElementById(inputid);
      inputNumber.value = diffvalue;
    } else {
      //first handle value should directly be written into the input field
      inputNumber.value = value;
    }
  }
});

1 个答案:

答案 0 :(得分:2)

谢谢Alon

那是我的错误。 将if更改为

if (handle  !== undefined  ) {

现在还捕获了ID为0的句柄