說明:驗證是否為空的前提首先要保證是在提交form之前驗證的,如果驗證結果有為空的項那么限制表單不能提交,并且提示驗證錯誤信息,驗證頁面屬性都不為空的情況下表單正常提交,這部分屬于JS部分的東西,與后臺業務邏輯層和控制層基本沒有任何關系。
限制提交的方法可以是在form標簽里限制,也可以是在submit按鈕上添加onclick事件限制:
<form action=".." method="post" onsubmit="return check();">
<input type="submit" value="提交" >
案例:
復制代碼 代碼如下:
<form action="./login.action" method="post" onsubmit="return check();">
<table>
<tr>
<td> </td>
<td>
<table >
<tr>
<td> </td>
<td>
<font color="#000000">用戶名:</font>
</td>
<td> <input type="text">
<span></span>
</td>
<tr> <td> </td>
<td nowrap>
<font color="#000000">密 碼:</font>
</td>
<td>
<input type="password">
<span></span>
</td>
</tr>
<tr>
<td colspan="4" nowrap>
<input type="submit" value=" 登 錄 " >
<input type="reset" value=" 重 置 ">
</td>
</tr>
</table>
</form>
復制代碼 代碼如下:
<script type="text/javascript" src="js/common/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
function check(){
var userName=$("#userName").val();
var password=$("#password").val();
alert(userName);
alert(password);
if(userName==""){
$("#checkUserName").html("<font color='red'>用戶名不能為空!</font>");
return false;
}
if(password==""){
$("#checkPassword").html("<font color='red'>密碼不能為空!</font>");
return false;
}
return true;
}
</script>
新聞熱點
疑難解答
圖片精選