请检查代码.html表单即使javascript返回false也会被提交。

<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;   
     } 
有帮助吗?

解决方案

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

OR

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>

其他提示

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top