Topic: 怎样判断checkbox的个数

  Print this page

1.怎样判断checkbox的个数 Copy to clipboard
Posted by: redhobor
Posted on: 2005-03-10 18:28

对javascript不熟,弄了半天没结果,只好求教于各位老大了。
<script language="javascript" type="">
<!--
//This function processes events of update or delete user
function sendData(num){
if(num=='1'){
document.frm.action="updateuser.jsp";
}
else if(num=='2'){
document.frm.action="deleteuser.jsp";
}
document.frm.submit();
}
<center>
<form name="frm" method="post">
<table width="75%" border="0">
<tr bgcolor="#CCCCCC">
<td> </td>
<td>ID</td>
<td>Surname</td>
<td>Forename</td>
<td>Privilege</td>
</tr>
<%
ResultSet rs = (ResultSet) request.getAttribute("userinfo");
while (rs.next()) {
usr_id = rs.getString("usr_id");
usr_surname = rs.getString("usr_surname");
usr_forename = rs.getString("usr_forename");
usr_privilege = rs.getString("usr_privilege");
out.print("<tr><td><input type=\"checkbox\" name=\"usr_id\" value=\"" + usr_id + "\"></td>");
out.print("<td>" + usr_id + "</td>");
out.print("<td>" + usr_surname + "</td>");
out.print("<td>" + usr_forename + "</td>");
out.print("<td>" + usr_privilege + "</td></tr>");
}
rs.close();
%>
</table>
<p> </p>
<p>
<input type="button" name="adduser" value="Add" onclick="doChange()">
<input type="submit" name="updateuser" value="Update" onclick="sendData('1')">
<input type="submit" name="deleteuser" value="Delete" onclick="sendData('2')">
</p>
</form>
CHECKBOX在Update时只能选取一个,而在删除时可以选择多个。所以我想了解用javascript方法来取到已经Checked的类别的个数。在Submit时,可以即时判断并显示window.alert()

2.Re:怎样判断checkbox的个数 [Re: redhobor] Copy to clipboard
Posted by: wmgreat
Posted on: 2005-03-10 20:51

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
alert(document.all.testcheckbox.length);
}
//-->
</SCRIPT>
</HEAD>

<BODY>
  <input type="checkbox" name="testcheckbox" value="1">1</input>
  <input type="checkbox" name="testcheckbox" value="2">2</input>
  <input type="checkbox" name="testcheckbox" value="3">3</input>
  <input type="checkbox" name="testcheckbox" value="4">4</input>
  <input type="checkbox" name="testcheckbox" value="5">5</input>
  <input type="button" name="Test" onclick="test()" value="Test"></input>
</BODY>
</HTML>

3.Re:怎样判断checkbox的个数 [Re: redhobor] Copy to clipboard
Posted by: chengbd
Posted on: 2005-03-11 00:37

只能检测出checkbox的总数,就差一点了,加油!

4.Re:怎样判断checkbox的个数 [Re: chengbd] Copy to clipboard
Posted by: redhobor
Posted on: 2005-03-11 04:05

我改写了,还是有问题。Length是取字段的长度还是个数?
<!--
//This function processes events of update or delete user
function sendData(num){
if(num=='1'){
var n=document.frm.usr_id.checked.length;
if(n!=1){
window.alert("You can choose one user to update once!");
}
else{
document.frm.action="updateuser.jsp";
}
}
else if(num=='2'){
document.frm.action="deleteuser.jsp";
}
document.frm.submit();
}
//-->
</script>

5.Re:怎样判断checkbox的个数 [Re: redhobor] Copy to clipboard
Posted by: wmgreat
Posted on: 2005-03-11 16:29

倒,都判断出来个数了,其他的还不好搞定阿!

6.Re:怎样判断checkbox的个数 [Re: redhobor] Copy to clipboard
Posted by: xiongjy
Posted on: 2005-03-12 23:03

自己写了一个,在固定个数和动态个数checkbox的情况下都可用的

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript" type="">
function getCheckedNum(checkboxname)
{
  if(checkboxname=="")
    return 0;
  var obj=document.getElementsByName(checkboxname);
  if (obj.length == 0 )
    return 0;
  var nCount=0;
  for(var j=0;j<obj.length;j++){
    var theobj=((typeof obj[j])=='undefined') ? obj : obj[j];
    if(theobj.checked){

      nCount++;
    }
  }
  return nCount;
}

</script>
</HEAD>

<BODY>
<input type="checkbox" name="testcheckbox" value="1">1</input>
<input type="checkbox" name="testcheckbox" value="2">2</input>
<input type="checkbox" name="testcheckbox" value="3">3</input>
<input type="checkbox" name="testcheckbox" value="4">4</input>
<input type="checkbox" name="testcheckbox" value="5">5</input>
<input type="button" name="Test" onclick="alert(getCheckedNum('testcheckbox'))" value="Test"></input>
</BODY>
</HTML>


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923