创建一个用于HTML表单中范围滑块的事件监听器。

huangapple go评论53阅读模式
英文:

Making an event listener for a range slider from an html form

问题

    document.getElementById("slider").onmouseup = function(){
        newWord = letterShifter(document.getElementById("slider").value, document.getElementById("wordInput").value.toLowerCase());
        document.getElementById("topcipher").innerText = newWord;
    };
英文:

I have been trying to get this eventlistener for my range input to work onmouseup after the client has selected the number on the slider though it won't pick it up.

window.onload = function(){ 
document.getElementById("wordInput").onkeyup = function(){
    
    sliderInt = parseInt(document.getElementById("slider").value);


    newWord = letterShifter(sliderInt, this.value.toLowerCase());
    document.getElementById("topcipher").innerText = newWord;
};

document.getElementById("slider").onmouseup = function(){
    newWord = letterShifter(document.getElementById("slider").value, document.getElementById(wordInput).toLowerCase());
    document.getElementById("topcipher").innerText = newWord;
};

The first wordInput eventlistener works (just takes a word from a text input) but the slider on mouse up one does not. What could I do to fix this please?

答案1

得分: 1

你没有发布完整的代码示例,但"onmouseup"事件在滑块上完美工作。

我的jsfiddle示例

<input type="range" min="1" max="100" value="50" class="slider" id="slider">
<span id="topcipher"></span>
window.addEventListener("load", (event) => {
  document.getElementById("slider").onmouseup = function() {
    sliderInt = parseInt(document.getElementById("slider").value);

    document.getElementById("topcipher").innerText = sliderInt;
  };  
});
英文:

You didn't post a full code example, but onmouseup event perfectly works on slider.

My example on jsfiddle:

&lt;input type=&quot;range&quot; min=&quot;1&quot; max=&quot;100&quot; value=&quot;50&quot; class=&quot;slider&quot; id=&quot;slider&quot;&gt;
&lt;span id=&quot;topcipher&quot;&gt;&lt;/span&gt;

 window.addEventListener(&quot;load&quot;, (event) =&gt; {
   document.getElementById(&quot;slider&quot;).onmouseup = function() {
    sliderInt = parseInt(document.getElementById(&quot;slider&quot;).value);

    document.getElementById(&quot;topcipher&quot;).innerText = sliderInt;
  };  
});

huangapple
  • 本文由 发表于 2023年2月7日 05:08:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定