日期:2014-05-16  浏览次数:20506 次

如何检测某个table 中的checkbox选中(个数不确定,从数据库读的)
如何检测某个table   中的checkbox选中(个数不确定,从数据库读的),并且还有N个这样的表,我的目的是检测其中一个table中的checkbox的选中情况

------解决方案--------------------
<script language= "javascript ">
function count(id)
{
o = document.getElementById(id).getElementsByTagName( "input ");
num = 0;
for(c=0; c <o.length; c++)
{
if(o[c].type== "checkbox " && o[c].checked) num++;
}
alert(num);
}
</script>
<table id= "t1 ">
<tr>
<td> <input type= "checkbox "> </td>
<td> <input type= "checkbox "> </td>
<td> <input type= "checkbox "> </td>
<td> <input type= "button " onclick= "count( 't1 ') " value= "检测 ">
</tr>
</table>
<table id= "t2 ">
<tr>
<td> <input type= "checkbox "> </td>
<td> <input type= "checkbox "> </td>
<td> <input type= "checkbox "> </td>
<td> <input type= "checkbox "> </td>
<td> <input type= "button " onclick= "count( 't2 ') " value= "检测 ">
</tr>
</table>
------解决方案--------------------
struts中的,将复选框放入一个字符串数组中checkID
<logic:iterate id= "data " name= "productKindData " scope= "request " indexId= "idx " offset= "0 ">
<tr>
<td width= "10% " align= "center ">
<input name= "checkID " type= "checkbox " value= " <%=String.valueOf(idx)%> "/>
</td>
</tr>
</logic:iterate>

用javascript判断的话用这个函数
function IsPitch(formitem , NAME ){
var obj = formitem.elements[NAME] ;
var length = formitem.elements[NAME].length;
for (var i = 0 ;i < length ; i++){
if (obj[i].checked){
return true ;
}
}
if (obj.value != " " && (typeof obj.length == "undefined ") ){
if (obj.checked){
return true ;
}else{
return false;
}
}
return false ;
}
其中formitem就是本表单,NAME就是checkID例如:
if (IsPitch(document.forms[0] , 'checkID ') ){alter( "操作!! ");}
用java判断就不用写了吧,checkID[]中存的就是选取的复选框,没选取的里面没有它的值,遍历就可以了