方法1:使用form onsubmit标签 return XXX()方法
<!--onsubmit-->
<form id="formid" name="myform" method='post' onsubmit="return checkUser();">
<table 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"/>
</div>
</td>
</table>
</form>
<script>
function checkUser() {
var result = document.getElementById("userid").value;
var password = document.getElementById("userpassid").value;
if (result == "") {
alert("用户名不能为空");
return false;
}
if (password == "") {
alert("密码不能为空");
return false;
}
}
debugger;
alert('Your data is correct.');
</script>
方法2:input onclick标签
<form id="formid" name="myform" method='post'>
<table 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">
<!--onclick-->
<input type="submit" value="登录" class="btn2" onclick="checkUser();"/>
</div>
</td>
</table>
</form>