function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("userpassid").value;
if(result == "" ){
alert("用户名不能为空");
return false;
}
if(password == "" ){
alert("密码不能为空");
return false;
}else{
return true;
}
}
在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" /></form>
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();
}
<input type="button" value="" class="btn2" onclick = "checkUser();" />
<form id="myform" name="myform" method="post" onsubmit="return sumbitTest();"
action="RegisterAction.action">
<table>
<tr>
<td>姓名:</td>
<td> <input type="text" name="name" /> </td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="sex" value="1"> 男
<input type="radio" name="sex" value="0"> 女
</td>
</tr>
<tr>
<td>年龄:</td>
<td>
<select name="age">
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit普通提交">
<input type="button" id="ajaxBtn" value="AJAX提交" />
<input type="button" id="jqueryBtn" value="jQuery提交" />
<input type="button" id="jsBtn" value="JS提交" />
<input type="submit" value="onSubmit提交" />
</td>
</tr>
</table>
</form>
<script type="text/javascript">
$(function() {
//ajax提交
$("#ajaxBtn").click(function() {
var params = $("#myform").serialize();
$.ajax( {
type : "POST",
url : "RegisterAction.action",
data : params,
success : function(msg) {
alert("success: " + msg);
}
});
})
//jQuery提交
$("#jqueryBtn").click(function(){
$("#myform").submit();
})
//js提交
$("#jsBtn").click(function(){
document.myform.action="RegisterAction.action";
document.myform.submit();
})
})
function sumbitTest(){
return true; //return false则不会提交
}
</script>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛