function checkDelForm() {
var uids = -1;
uids = document.getElementsByName("delUids").length;
if (uids == -1) {
alert("請選擇要刪除的用戶");
return false;
} else {
return true;
}
}
<form name="userform" action="servlet/DeleteUserServet" method="post"
onsubmit="return checkDelForm()">
<table align="center" border="1" width="440px">
<tr>
<td colspan="6" align="center">用戶資料</td>
</tr>
<tr>
<td align="center">用戶姓名</td>
<td align="center">用戶性別</td>
<td align="center">出生日期</td>
<td align="center">用戶住址</td>
<td colspan=2 align="center">操作</td>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("uname")%></td>
<td><%=rs.getString("usex")%></td>
<td><%=rs.getString("ubirth")%></td>
<td><%=rs.getString("uaddr")%></td>
<td><a
href="UserListDetail.jsp?uid=<%=rs.getInt("uid")%>&&uname=<%=rs.getString("uname")%>&&usex=<%=rs.getString("usex")%>&&ubirth=<%=rs.getString("ubirth")%>&&uaddr=<%=rs.getString("uaddr")%>">編輯</a>
</td>
<td><input type="checkbox" name="delUids"
value="<%=rs.getInt("uid")%>"></td>
</tr>
<%
}
conn.close();
rs.close();
stmt.close();
%>
<tr>
<td colspan="6" align="center"><input type="submit" value="刪除"
style="width: 82px; "> <font color="green"> ${msg} </font>
</td>
</tr>
</table>
</form>
你只是判斷delUids有多少個。而且也判斷錯了,是==0,不是==-1,改成下面的
function checkDelForm() {
var cbs = document.getElementsByName("delUids"), len = cbs.length, ok = false;
for (var i = 0; i < len; i++)
if (cbs[i].checked) {
ok = true; break;
}
if (!ok) {
alert("請選擇要刪除的用戶");
return false;
} else {
return true;
}
}