1

我目前正在使用谷歌地图并尝试使用输入验证。我要求用户使用 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;
                           }
                       }
                   }
4

2 回答 2

3

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

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;
    }
}
于 2012-07-23T00:23:29.923 回答
0
   function inputIsValid() {
   $value = document.getElementById("txtZoom").value;

   if (($value  < 0) && ($value  > 20)) {
   alert("Please enter a value between 0 and 20");
   return false;
   }
于 2012-07-23T00:23:01.220 回答