如何从数组中获取范围滑块内的值

时间:2019-06-11 08:26:27

标签: javascript arrays slider range

我无法获得链接到滑块的数组值(字母)。它给出一个数字而不是字母。 顺便说一句,仅Javascript是目标。

我尝试给它一个createSlider.value = allTheLetters,但是它不起作用。

// Creating the Alphabet Array

var allTheLetters =         ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; 

 // Creating button 
  var button = document.createElement("button");
  button.style.display = "block";
  button.id = "sliderButton";
  var sliderButtonId = document.getElementById("sliderButton");
  button.innerHTML = "Add new letter";
 // End creating Button

 // Creating slider and displaying it to the DOM
  var sliderParent = document.getElementById("target");
  var createSlider = document.createElement("input");
  createSlider.type = "range";
  createSlider.min = 0;
  createSlider.max = allTheLetters.length;
  sliderParent.appendChild(createSlider);
  sliderParent.appendChild(button);
 // End slidercreation

createSlider的值现在是50,我希望它以字母A开头并最大为字母Z。

1 个答案:

答案 0 :(得分:0)

结果是您无法在滑块内获得非数值,因此您必须获取滑块位置的值并将其链接到您创建的数组,如下所示:

// Creating slider and displaying it to the DOM
            var sliderParent = document.getElementById("target");
            var createSlider = document.createElement("input");
            createSlider.type = "range";
            createSlider.min = 0;
            createSlider.max = allTheLetters.length-1;
            sliderParent.appendChild(createSlider);
            sliderParent.appendChild(button);

            // get Value of slider location
            var sliderResult = createSlider.value;
            var theLetterIWant = allTheLetters[sliderResult];

            console.log(theLetterIWant);
            // End slidercreation