如何在Javascript中检查html滑块值? (值为1或值+ 1)

问题描述 投票:0回答:2

我在HTML中有这个滑块,我想知道如何检查滑块的先前值是否与当前值相等。如果你知道怎么做,请告诉我。谢谢你的时间。

var target = document.getElementById("myTarget");
let previous;

function foo(myValue) {
     
    //console.log(myValue);
     
    //Update SPAN
    target.innerHTML = myValue; //EXPECTED = CURRENT VALUE

    console.log(previous);

    if(previous === myValue){
       //Statements
       console.log("same");
    }else{
       //Statemets
       //previous = myValue;
       console.log("different");
    }
}
<input id="mySlider" type="range" value="0" min="0" max="5" step="1" oninput="foo(this.value);">
<span id="myTarget">0</span>

enter image description here

javascript html5
2个回答
1
投票

const slider = document.getElementById('mySlider');
const output = document.getElementById('output');

var initialValue = 2; // set however you want

slider.addEventListener('input', function (event) {
    let outcome = "'"+initialValue+"' and '"+this.value+"' are ";
    if( parseInt(this.value)===initialValue){
        outcome+= "equal!"
    } else{
        outcome+= "not equal"
    }
    output.innerHTML = outcome
}, false);
<input id="mySlider" type="range" value="0" min="0" max="5" step="1"/>


<div id="output">
  <em>Not run yet</em>
</div>

0
投票

感谢@Martijn回答我能够创建一个这样的循环:

var slider = document.getElementById("slider-element_BV");

    slider.addEventListener('input', function(event){

    RAYS_CLASS.Ray_Techniques.RAYS_TECHNIQUES_MANAGER.BV().BV_REMOVE();

    for(var i=this.min; i<=this.max; i++) {
        if(parseInt(this.value) === i){
            RAYS_CLASS.Ray_Techniques.RAYS_TECHNIQUES_MANAGER.BV().BV_Subdivide(i);
        }
    }
});

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.