一段简单的提交代码<form action="conn.jsp" method="post" name="myform" id="myform">
  <table width="44%"  border="2" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="33%">用户名:</td>
      <td width="67%"><input type="text" name="username"></td>
    </tr>
    <tr>
      <td>密 码:</td>
      <td><input type="password" name="userpass"></td>
    </tr>
  </table>
  <br>
  <input type="submit" name="Submit" value="提交">
</form>
如何用javaScript判断username和userpass的提交字段是否为空.

解决方案 »

  1.   

    myform.username.value == ""
    可能需要trim,而JS没有trim函数,需要自己写。
      

  2.   

    trim函数?
    我去找找.
    在asp里面通过的javaScript代码
    <script Language="javaScript">
        function  validate()
        {
            if  (document.myform.username.value=="")
            {
                alert("用户名不能为空.");
                document.myform.username.focus();
                return false ;
            }
    if  (document.myform.userpass.value=="")
            {
                alert("用户名不能为空.");
                document.myform.userpass.focus();
                return false ;
            }            return  true;
        }
    </script>
    放在这里根本没有作用
      

  3.   

    呵呵.问题解决了。
    <form action="conn.jsp" method="post" name="myform" onsubmit="return  validate()">
    就可以了。
    谢谢2位