我想做,验证输入框中如果为空不能提交,但是为什么我加了return false 但是还能提交到那个页面呢
<script type="text/javascript">
document.write("wode");
document.close();
function jiancha(){
var username=document.getElementById("username").value;
var userpassword=document.getElementById("userpassword").value;
if(username==""){
alert ("用户名不可为空");
return false;
}
if(userpassword==""){

alert ("密码不可为空");
return false;
}
}</script>
<body>
<form id="form1" name="form1" method="post" action="hhh.html">
  <p>
    <label for="textfield"></label>
  
  用户名
  <input type="text" name="username" id="usrname" />
  </p>
  <p>密码
    <label for="textfield2"></label>
    <input type="text" name="userpassword" id="userpassword" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="提交" onclick="jiancha();"  />
    <input type="reset" name="button2" id="button2" value="重置" />
  </p>
</form>

解决方案 »

  1.   


    <script type="text/javascript">
    function jiancha(){
      var username = document.getElementById("username").value;
      var userpassword = document.getElementById("userpassword").value;
      if(username==""){
        alert ("用户名不可为空");
        return false;
      }
      if(userpassword==""){
        alert ("密码不可为空");
        return false;
      }
      return true;
    }</script>
    <body>
    <form id="form1" name="form1" method="post" action="hhh.html"  onsubmit="return jiancha();">
      <p>
      <label for="textfield"></label>
       
      用户名
      <input type="text" name="username" id="usrname" />
      </p>
      <p>密码
      <label for="textfield2"></label>
      <input type="text" name="userpassword" id="userpassword" />
      </p>
      <p>
      <input type="submit" name="button" id="button" value="提交" />
      <input type="reset" name="button2" id="button2" value="重置" />
      </p>
    </form>
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function jiancha(){
      var username = document.getElementById("usrname").value;
      var userpassword = document.getElementById("userpassword").value;  if(username==""){
        alert ("用户名不可为空");
        return false;
      }
      if(userpassword==""){
        alert ("密码不可为空");
        return false;
      }
      return true;
    }</script>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="a.jsp"  >
      <p>
      <label for="textfield"></label>
       
      用户名
      <input type="text" name="username" id="usrname" value=""/>
      </p>
      <p>密码
      <label for="textfield2"></label>
      <input type="text" name="userpassword" id="userpassword" value=""/>
      </p>
      <p>
      <input type="submit" name="button" id="button" value="提交" onclick="return jiancha()"/>
      <input type="reset" name="button2" id="button2" value="重置" />
      </p>
    </form>
    </body>
    </html>
      

  3.   

    问题出现,先从关键的细节出着手,检查是否出错,比如你的id设置上就有问题,取不到任何值。
    如果问题还在,找你的程序关键点, <input type="submit" name="button" id="button" value="提交" onclick="jiancha();" />
    这是你的
    <input type="submit" name="button" id="button" value="提交" onclick="return jiancha()"/>
    这是关键。
    加油哦!
      

  4.   

    return 是有返回值,你不写return的意思就是不需要返回值,返回true或false当然都没效果了。