在form表单里写成这样:
<form id="formid" name= "myform" method = 'post' action = 'user_login_submit.action' onsubmit = "return checkUser();" >
<table width="100%" border="0"> <tr> <td width="60" height="40" align="right">用户名 </td> <td><input type="text" value="" class="text2" name = "username" id = "userid"/></td> </tr> <tr> <td width="60" height="40" align="right">密 码 </td> <td><input type="password" value="" class="text2" name = "userpass" id = "userpassid"/></td> </tr> <tr> <td width="60" height="40" align="right"> </td> <td><div class="c4"> <input type="submit" value="" class="btn2" /> 方法二: function checkUser(){ var result = document.getElementById("userid").value; var password = document.getElementById("passid").value; if(result == "" ){ alert("用户名不能为空"); return false; } if(password == "" ){ alert("密码不能为空"); return false; } document.getElementById("formid").submit();// 此处发送了表单 } form表格的写法,需要写id <form id="formid" method = 'post' action = 'user_login_submit.action' > button按钮的写法如下:<input type="button" value="" class="btn2" onclick = "checkUser();" />
//除了原博的上述两种办法,本人还知道一种
方法三:
<button type="submit" id="loginBtn">Login</button>
...
(与方法二类似,只是不需要在js方法里写上最后那行提交表单的命令)