gpt4 book ai didi

javascript - slider 未定义/NaN 错误

转载 作者:行者123 更新时间:2023-11-30 11:44:56 24 4
gpt4 key购买 nike

我有下面的 slider :

var rangeValue;
var chanceoflive;
var inputElement = document.querySelector('.range-input');

inputElement.addEventListener('change', function() {
var rangeValue = parseInt(this.value);
chanceoflive = chanceoflive || 0;

chanceoflive = rangeValue > 51 ? 2 : 4;
}
);

function handleClick(){
alert(chanceoflive);
}
<main>
<form oninput="output.value = Math.round(range.valueAsNumber / 1)">
<h2>
Choose the Height of Your Building
</h2>
<div class="range">
<input name="range" type="range" min="0" max="100" class="range-input">
<div class="range-output">
<output id="output" class="output" name="output" for="range">
50
</output>
</div>
</div>
</form>
</main>

<button type="button" onclick="handleClick()" class="submit">Submit</button>

应该发生的是,如果 slider 低于 51,它会发出警报 4。如果 slider 不低于 51,它会发出警报 2。尽管如此,如果 slider 在 50,它会发出未定义或 NaN 警报未定义。为什么会这样,我该如何解决?

最佳答案

现在开始吧

只需要初始化 chanceoflive

var rangeValue;
var chanceoflive =0;
var inputElement = document.querySelector('.range-input');

inputElement.addEventListener('change', function() {
var rangeValue = parseInt(this.value);
chanceoflive = chanceoflive || 0;

chanceoflive = rangeValue > 51 ? 2 : 4;
}
);

function handleClick(){
alert(chanceoflive);
}
<main>
<form oninput="output.value = Math.round(range.valueAsNumber / 1)">
<h2>
Choose the Height of Your Building
</h2>
<div class="range">
<input name="range" type="range" min="0" max="100" class="range-input">
<div class="range-output">
<output id="output" class="output" name="output" for="range">
50
</output>
</div>
</div>
</form>
</main>

<button type="button" onclick="handleClick()" class="submit">Submit</button>

关于javascript - slider 未定义/NaN 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com