0

我试图找出一种简单的方法来通过 javascript 中的 while 循环来限制对字段的尝试次数。只是似乎不想循环,但验证仍在工作。这是我试图使用的代码:

 function validateForm() {
     x=document.forms["Form"]["name"].value;
     var i=0;
     var sum=0;
     do {
         sum += i;
         i++;
     }
     while (i < 3)

     if (x==null || x=="") {
         alert("First name must be filled out");
         return false;
     }
}

提前感谢您的任何想法。

4

1 回答 1

0

您必须将变量存储在函数之外:

 var numOfTries = 0;

 function validateForm() {

     if(++numOfTries > 3) {
         alert('You have reached the maximum number of tries!');
         location.replace("error_page.html"); // do something about it
     }

     x = document.forms["Form"]["name"].value;

     if (x == null || x == "") {
         alert("First name must be filled out");
         return false;
     }
于 2013-09-12T02:36:05.197 回答