日期:2014-05-18  浏览次数:20742 次

如何遍历页面checkbox控件,得到已选中的
如何遍历页面checkbox控件,得到已选中的

------解决方案--------------------
javascript:
var total=form.checkboxname.length;
for(var i=0;i <total;i++){
if(form.checkboxname[i].checked){ //checkboxname(i)??
alert( " ");
}
}
------解决方案--------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
<Script>
function addtable(){
var aa;
for (var i=0;i <form1.elements.length;i++)
{
var e = form1.elements[i];
if(e.id== "iddel ")
{
if(e.checked == true)
{
aa=document.form1.elements[ "delid "][i].value;
alert(aa);
}
}
}
}

</SCRIPT>
<body>
<form name= "form1 ">
<input type= "checkbox " value= "1 " name= "delid " id= "iddel " >
<input type= "checkbox " value= "2 " name= "delid " id= "iddel ">
<input type= "checkbox " value= "3 " name= "delid " id= "iddel " >
<input type= "checkbox " value= "4 " name= "delid " id= "iddel " >
<input name= "add " type= "button " value= "提交 " onclick= "addtable() " />
</form>
</body>
</html>
------解决方案--------------------
String[] strLogIDs = request.getParameterValues( "ids ");
------解决方案--------------------
<% session.setAttribute( "C22 ", request.getParameterValues( "C22 "));
//C22是你checkbox的name
String[] arrChecked = (String[]) request.getParameterValue( "C22 ");
String strChecked = " ";
if (arrChecked != null) {
for (int i = 0; i < arrChecked.length; i++) {
strChecked += arrChecked[i] + ", ";
}
}%>
html...

<script language= "JavaScript ">
var strChecked = <%= " ' "+strChecked+ " ' "%> ;
for (var i=0;i <form1.C22.length;i++) {
if (strChecked.indexOf(form1.C22[i].value+ ", ")> -1) {
form1.C22[i].checked = true;
}
}
</script>