gpt4 book ai didi

javascript - 警报错误值 slider 输出

转载 作者:行者123 更新时间:2023-12-01 04:10:23 25 4
gpt4 key购买 nike

我有一个 slider ,可以在单击按钮时提醒输出,但它总是在应该提醒 1、2、3 或 4 时提醒 2。这是我的代码:

var chanceoflive3;
var inputElement = document.querySelector('.range-input');
var submitElement = document.querySelector('.submit');

submitElement.addEventListener('click', function() {
document.getElementById('paragraph').innerHTML = (chanceoflive3||0);
localStorage.setItem("chanceoflive3", chanceoflive3);
});

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

if (rangeValue == 1) {
chanceoflive3 = 1;
}

else if (rangeValue > 1 && rangeValue < 51 ) {
chanceoflive3 = 2;
}

else if (rangeValue > 50 && rangeValue < 100) {
chanceoflive3 = 3;
}

else if (rangeValue == 100) {
chanceoflive3 = 4;
}
}
);

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

<div class="wrapper">
<button align=center onclick="handleClick()">
<canvas width="200" height="50" id="canvas" align=center></canvas>
<hover></hover>
<span id="submit">SUBMIT</span>
</button>
</div>

如果 slider 设置为 1,则应发出警报 1;如果 slider 在 2 到 50 之间,则应发出警报 2;如果 slider 在 51 到 99 之间,则应发出警报 3;如果 slider 设置为 100,则应发出警报 4。 2、无论如何。为什么会这样,我该如何解决?

最佳答案

试试这个:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<main>
<form oninput="output.value = Math.round(range.valueAsNumber / 1)">
<h2>
Choose the Length of Your House
</h2>
<div class="range">
<input name="range" type="range" min="1" max="100" class="range-input">
<div class="range-output">
<output id="output" class="output" name="output" for="range">
50
</output>
</div>
</div>
</form>
</main>

<div class="wrapper">
<button align=center onclick="handleClick()">
<canvas width="200" height="50" id="canvas" align=center></canvas>
<hover></hover>
<span id="submit">SUBMIT</span>
</button>
</div>
<script type="text/javascript">
var inputElement = document.querySelector('.range-input');
var submitElement = document.querySelector('#submit');
var chanceoflive3 = chanceoflive3 || 0;

submitElement.addEventListener('click', function () {
document.getElementById('paragraph').innerHTML = (chanceoflive3 || 0);
localStorage.setItem("chanceoflive3", chanceoflive3);
});

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

if (rangeValue == 1) {
chanceoflive3 = 1;
}

else if (rangeValue > 1 && rangeValue < 51) {
chanceoflive3 = 2;
}

else if (rangeValue > 50 && rangeValue < 100) {
chanceoflive3 = 3;
}

else if (rangeValue == 100) {
chanceoflive3 = 4;
}
});

function handleClick() {
alert(chanceoflive3);
}
</script>
</body>
</html>

关于javascript - 警报错误值 slider 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561146/

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