0

即使 javascript 返回 false,请检查代码 .html 表单是否已提交。

<form id="form1" name="form1"   method="post" action="sub.jsp" onsubmit="return getValue()">
<input type="text" id="userName" name="userName"   onkeyup="return getValue()" />
<input type="submit" name="Submit" value="Submit" />
</form>

    <script type="text/javascript" >
    function getValue()
      {
        var userName=document.getElementById("userName");
            document.getElementById("userNamemsg").innerHTML="";
              if(userName.value=="")
              {
             var mdiv=document.getElementById("userNamemsg");
                  mdiv.innerHTML="Error:Required Fields cannot be blank!";
                  form.userName.focus();
                  return false;
               }
              return true;   
     } 
4

3 回答 3

3

1)尝试将线路更改form.userName.focus();document.form1.userName.focus();

或者

2)尝试从函数本身提交:

<input type="button" name="Submit" value="Submit" onclick="getValue()" />

<script type="text/javascript" >
function getValue()
  {
        var userName=document.getElementById("userName");
        document.getElementById("userNamemsg").innerHTML="";
          if(userName.value=="")
          {
              var mdiv=document.getElementById("userNamemsg");
              mdiv.innerHTML="Error:Required Fields cannot be blank!";
              document.form1.userName.focus();//I think this is the problem
              return false;
           }
          document.form1.submit();
 }
 </script>
于 2009-09-13T04:32:23.617 回答
1

我认为您的 JavaScript 代码中有错误发生在您的 return 语句之前。修复这些错误,您的代码应该可以工作。

于 2009-09-13T04:28:48.240 回答
-1

或者,您使提交按钮上的单击处理程序返回 false。

于 2009-09-13T06:10:35.010 回答