gpt4 book ai didi

javascript - 验证输入在javascript中的2个数字之间

转载 作者:行者123 更新时间:2023-11-30 13:17:15 26 4
gpt4 key购买 nike

我目前正在使用 Google map 并尝试使用输入验证。我要求用户使用 0 到 20 之间的数字来设置我的位置的缩放比例。

下面是我使用的代码。第一个 if 语句适用于任何超过 20 的数字,但第二个语句在我使用像 0 和 -1 这样的数字时不起作用(例如。)

有什么解决这个问题的建议吗?

function inputIsValid() {

console.log("Inside inputIsValid function");

//Check for Above 20
if (document.getElementById("txtZoom").value > 20) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;

//Check for Number below 0
if (document.getElementById("txtZoom").value < 0) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;
}
}
}

最佳答案

问题是您将第二个检查嵌套在第一个检查中,因此永远不会到达。试试这个:

function inputIsValid() {
var zoomValue = document.getElementById("txtZoom").value;

if (zoomValue > 20 || zoomValue < 0) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;
}
}

关于javascript - 验证输入在javascript中的2个数字之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605103/

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