<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Web Project</title>
        <script>
            var flag;
            function checked(){
                var username = document.getElementById("userId").value;
                var pwd = document.getElementById("pwdid").value;
                if (username == "") {
                   document.getElementById("userIds").innerHTML="用户名不能为空";
    flag = false;
                }
                if (pwd == "") {
                   
                     document.getElementById("pwdids").innerHTML="密码不能为空";
  flag = false;
                }
alert(flag);
                return flag;
            }
        </script>
    </head>
    <body>
        <form action="a.html" method="get" onsubmit="return checked()">
            <input type="text" name="username" id="userId"/>
用户名 <input type="text" name="usernames" id="userIds"/>
 <input type="text" name="pwd" id="pwdid"/>
密码 <input type="text" name="pwds" id="pwdids"/>
 <input type="submit" value="提交"/>
        </form>
    </body>
</html>当我用户名 密码都为空的时候 为什么还能登录那

解决方案 »

  1.   

    代码没问题 你用的什么浏览器?
    另外:document.getElementById("userIds").innerHTML="用户名不能为空";
    text框是没有innerHTML的
      

  2.   

    代码是有问题的就是楼上所说的,innerHTML用法出错了
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>New Web Project</title>
        <script>  var flag=true;
      function checked(){
      var username = document.getElementById("userId").value;
      var pwd = document.getElementById("pwdid").value;
      if (username == ""){
     //      document.getElementById("userIds").innerHTML="用户名不能为空";
           alert("用户名不能为空");
           flag = false;
      }
      if(pwd==""){ 
       //    document.getElementById("pwdids").innerHTML="密码不能为空"; 
           alert("密码不能为空");
           flag = false;
      }
      //alert(flag);
      return flag;
      }
      </script>
      </head>
      <body>
      <form action="a.html" method="get" onsubmit="return checked()">
      <input type="text" name="username" id="userId"/>用户名 
      <input type="text" name="usernames" id="userIds"/>
      <input type="text" name="pwd" id="pwdid"/>密码
      <input type="text" name="pwds" id="pwdids"/>
      <input type="submit" value="提交"/>
      </form>  </body>
    </html>
      

  3.   


    function checked(){
      var username = document.getElementById("userId").value;
      var pwd = document.getElementById("pwdid").value;
      if (username == "") {
      document.getElementById("userIds").value="用户名不能为空";
    flag = false;
      }
      if (pwd == "") {
       
      document.getElementById("pwdids").value="密码不能为空";
    flag = false;
      }
    alert(flag);
      return flag;
      }
    input没有innerHTML,直接用value就行
      

  4.   

    请高手再去这个连接 给解决一道题 http://topic.csdn.net/u/20111008/14/5f9a5ab6-fe37-4102-b3d5-d8bfb5ad9a86.html 解决完一块给分 谢谢