說明:驗(yàn)證是否為空的前提首先要保證是在提交form之前驗(yàn)證的,如果驗(yàn)證結(jié)果有為空的項(xiàng)那么限制表單不能提交,并且提示驗(yàn)證錯(cuò)誤信息,驗(yàn)證頁面屬性都不為空的情況下表單正常提交,這部分屬于JS部分的東西,與后臺(tái)業(yè)務(wù)邏輯層和控制層基本沒有任何關(guān)系。
限制提交的方法可以是在form標(biāo)簽里限制,也可以是在submit按鈕上添加onclick事件限制:
<form action=".." method="post" onsubmit="return check();">
<input type="submit" value="提交" onclick="return check()" >
案例:
<table >
<tr>
<td width="60"> </td>
<td>
<font color="#000000">用戶名:</font>
</td>
<td align="left"> <input name="userInfo.userName" id="userName" type="text">
<span id="checkUserName"></span>
</td>
<tr> <td> </td>
<td align="right" nowrap>
<font color="#000000">密 碼:</font>
</td>
<td align="left">
<input name="userInfo.password" id="password" type="password">
<span id="checkPassword"></span>
</td>
</tr>
<tr>
<td colspan="4" align="center" nowrap>
<input type="submit" id="btnLog" value=" 登 錄 " >
<input type="reset" value=" 重 置 ">
</td>
</tr>
</table>
</form>
<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>
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注