为什么我点击登陆按钮不提示用户名为空,密码为空?
<body>
<script type="text/javascript">
function check(){
if(document.form.name.value == ""){
alert("用户名为空");
document.form.name.focus();
return false;
}
if(document.form.password.value == ""){
alert("密码为空");
document.form.password.focus();
return false;
}
return true;
}

function init(){
alert("hello");
var flag = '<%=request.getParameter("FLAG")%>';
if(flag=='Error'){
alert("表单为空);
}
if(flag=='ERRORINFO'){
alert("用户名或密码错误");
}
}</script>
<table width="681" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:120px">
  <tr>
    <td width="353" height="259" align="center" valign="bottom" background="images/login_1.gif"><table width="90%" border="0" cellspacing="3" cellpadding="0">
      <tr>
        <td align="right" valign="bottom" style="color:#05B8E4">Power by <a href="http://www.njrsrc.com" target="_blank">南京人事人才网</a> Copyright 2010</td>
      </tr>
    </table></td>
    <td width="195" background="images/login_2.gif"><table width="190" height="106" border="0" align="center" cellpadding="2" cellspacing="0">
      <form method="post" name="NETSJ_Login" onClick= "return check();" action="login.action" >
            <tr>
              <td height="50" colspan="2" align="left">&nbsp;</td>
            </tr>
            <tr>
              <td width="60" height="30" align="left">登陆名称</td>
              <td><input name="name" type="TEXT" style="background:url(images/login_6.gif) repeat-x; border:solid 1px #27B3FE; height:20px; background-color:#FFFFFF" id="UserName"size="14"></td>
            </tr>
            <tr>
              <td height="30" align="left">登陆密码</td>
              <td><input name="password" TYPE="PASSWORD" style="background:url(images/login_6.gif) repeat-x; border:solid 1px #27B3FE; height:20px; background-color:#FFFFFF" id="Password" size="16"></td>
            </tr>
            <tr>
              <td height="30"> 验 证 码 </td>
  <td><input name="Code" type="text" id="Code" size="4" style="background:url(images/login_6.gif) repeat-x; border:solid 1px #27B3FE; height:20px; background-color:#FFFFFF" maxlength="4">
  <img src="images/VerifyCode.png" width="50" height="24" />
      </td>
            </tr>
            <tr>
              <td height="40" colspan="2" align="center"><img src="images/tip.gif" width="16" height="16"> 请勿非法登陆!</td>
          <tr>
              <td colspan="2" align="center"><input type="submit" name="submit" style="background:url(images/login_5.gif) no-repeat" value=" 登  陆 " onClick= "return check();"> 
  <input type="reset" name="Submit" style="background:url(images/login_5.gif) no-repeat" value=" 取  消 "></td>
            <tr>
              <td height="5" colspan="2"></td>
        </form>
    </table></td>
    <td width="133" background="images/login_3.gif">&nbsp;</td>
  </tr>
  <tr>
    <td height="161" colspan="3" background="images/login_4.gif"></td>
  </tr>
</table>
</body>

解决方案 »

  1.   

      <form method="post" name="NETSJ_Login" onsubmit= "return check();" action="login.action" >
      

  2.   

    <body>
    <script type="text/javascript">
    function check(form){
    if(form.name.value == ""){
    alert("用户名为空");
    form.name.focus(); return false;
    }
    if(form.password.value == ""){
    alert("密码为空");
    form.password.focus();
    return false;
    }
    return true;
    }</script>
    <form method="post" name="NETSJ_Login" onsubmit= "return check(this);" action="login.action" >
    <input name="name" type="TEXT" id="UserName">
    <input name="password" TYPE="PASSWORD" id="Password">
    <input type="submit" name="submit" value=" 登 陆 ">  
    </form>
      

  3.   

    首先在from表单处的触发事件改为onsubmit<form method="post" name="NETSJ_Login" onsubmit= "return check(this);" action="login.action" >把submit登录按钮的onclick事件去掉
    onsubmit= "return check(this)this代表当前表单对象,然后通过它去找表单内的控件名称就OK了function check(val){
    if(val.name.value == ""){
    alert("用户名为空");
    val.name.focus();
    return false;
    }
    if(val.password.value == ""){
    alert("密码为空");
    val.password.focus();
    return false;
    }
    return true;
    }