■■投票程序中好几组单选框的有选择全选问题

apoint 2010-05-27 11:06:27
情况描述:
有一个投票,每行是一个人的记录,分三列代表三个题目,每一题都是单选。
因此每一个人(每行),对应有三组共9个单选按纽 ,命名规则为 test(题号)_(被投人序号)如 test2_3 即第3个人,第二题的一组单选。

问题
在最下面设计了几个复选框,比如 全选本题A。要求勾此勾时,把所有人的这道题的A项选中。

求高手赐教。谢谢,高分求。


----------------源代码开始,请拷入一个htm文件源文件中

<table border="1" width="100%" id="table1">
<tr>
<td width="112">序号</td>
<td width="158">命名 test1_序号</td>
<td width="148">test2_序号</td>
</tr>
<tr>
<td width="112">1</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_1"> 
B<input type="radio" value="B" name="test1_1">  C<input type="radio" value="C" name="test1_1"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_1"> 
B<input type="radio" value="B" name="test2_1">  C<input type="radio" value="C" name="test2_1"></p>
</td>
</tr>
<tr>
<td width="112">2</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_2"> 
B<input type="radio" value="B" name="test1_2">  C<input type="radio" value="C" name="test1_2"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_2"> 
B<input type="radio" value="B" name="test2_2">  C<input type="radio" value="C" name="test2_2"></p>
</td>
</tr>
<tr>
<td width="112">3</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_3"> 
B<input type="radio" value="B" name="test1_3">  C<input type="radio" value="C" name="test1_3"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_3"> 
B<input type="radio" value="B" name="test2_3">  c<input type="radio" value="C" name="test2_3"></p>
</td>
</tr>
<tr>
<td width="112"> </td>
<td width="158">
<p>本题全选A<input type="checkbox" name="C1" value="ON"></p>
<p>本题全选B<input type="checkbox" name="C2" value="ON"></p>
<p>本题全选<span lang="en">c</span><input type="checkbox" name="C3" value="ON"><span lang="en"> </span></p>
</td>
<td width="148">
<p>本题全选A<input type="checkbox" name="C4" value="ON"></p>
<p>本题全选B<input type="checkbox" name="C5" value="ON"></p>
<p>本题全选<span lang="en">c</span><input type="checkbox" name="C6" value="ON"><span lang="en"> </span></p></td>
</tr>
</table>

------------------------------------源代码结束,请拷入一个htm文件源文件中
...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shan1119 2010-05-27
  • 打赏
  • 举报
回复
<table border="1" width="100%" id="table1">
<tr>
<td width="112">序号</td>
<td width="158">命名 test1_序号</td>
<td width="148">test2_序号</td>
</tr>
<tr>
<td width="112">1</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_1"> 
B<input type="radio" value="B" name="test1_1">  C<input type="radio" value="C" name="test1_1"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_1"> 
B<input type="radio" value="B" name="test2_1">  C<input type="radio" value="C" name="test2_1"></p>
</td>
</tr>
<tr>
<td width="112">2</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_2"> 
B<input type="radio" value="B" name="test1_2">  C<input type="radio" value="C" name="test1_2"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_2"> 
B<input type="radio" value="B" name="test2_2">  C<input type="radio" value="C" name="test2_2"></p>
</td>
</tr>
<tr>
<td width="112">3</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_3"> 
B<input type="radio" value="B" name="test1_3">  C<input type="radio" value="C" name="test1_3"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_3"> 
B<input type="radio" value="B" name="test2_3">  c<input type="radio" value="C" name="test2_3"></p>
</td>
</tr>
<tr>
<td width="112"> </td>
<td width="158">
<p>本题全选A<input type="checkbox" name="C1" value="ON"></p>
<p>本题全选B<input type="checkbox" name="C2" value="ON"></p>
<p>本题全选<span lang="en">c</span><input type="checkbox" name="C3" value="ON"><span lang="en"> </span></p>
</td>
<td width="148">
<p>本题全选A<input type="checkbox" name="C4" value="ON"></p>
<p>本题全选B<input type="checkbox" name="C5" value="ON"></p>
<p>本题全选<span lang="en">c</span><input type="checkbox" name="C6" value="ON"><span lang="en"> </span></p></td>
</tr>
</table>
<script>
var chk=document.getElementsByTagName("input");
for(var i=0;i<chk.length;i++){
if(chk[i].type=="checkbox"){
chk[i].onclick=function(){
var id=parseInt(this.name.substring(1));
var qId=Math.ceil(id/3);
var aId=id%3;

if(aId==0)aId=3;
document.getElementsByName("test"+qId+"_1")[aId-1].checked=this.checked;
document.getElementsByName("test"+qId+"_2")[aId-1].checked=this.checked;
document.getElementsByName("test"+qId+"_3")[aId-1].checked=this.checked;
for(var j=1;j<=3;j++)
if(this.name!=("C"+(j+(qId-1)*3)))document.getElementsByName("C"+(j+(qId-1)*3))[0].checked=false;
}
}
}
</script>
vnetcbd 2010-05-27
  • 打赏
  • 举报
回复



<table border="1" width="100%" id="table1">
<tr>
<td width="112">序号</td>
<td width="158">命名 test1_序号</td>
<td width="148">test2_序号</td>
</tr>
<tr>
<td width="112">1</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_1"> 
B<input type="radio" value="B" name="test1_1">  C<input type="radio" value="C" name="test1_1"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_1"> 
B<input type="radio" value="B" name="test2_1">  C<input type="radio" value="C" name="test2_1"></p>
</td>
</tr>
<tr>
<td width="112">2</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_2"> 
B<input type="radio" value="B" name="test1_2">  C<input type="radio" value="C" name="test1_2"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_2"> 
B<input type="radio" value="B" name="test2_2">  C<input type="radio" value="C" name="test2_2"></p>
</td>
</tr>
<tr>
<td width="112">3</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_3"> 
B<input type="radio" value="B" name="test1_3">  C<input type="radio" value="C" name="test1_3"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_3"> 
B<input type="radio" value="B" name="test2_3">  c<input type="radio" value="C" name="test2_3"></p>
</td>
</tr>
<tr>
<td width="112"> </td>
<td width="158">
<p>本题全选A<input type="checkbox" name="C1" value="ON" onclick="selectid(0,1);"></p>
<p>本题全选B<input type="checkbox" name="C2" value="ON" onclick="selectid(1,1);"></p>
<p>本题全选<span lang="en">c</span><input type="checkbox" name="C3" value="ON" onclick="selectid(2,1);"><span lang="en"> </span></p>
</td>
<td width="148">
<p>本题全选A<input type="checkbox" name="C4" value="ON" onclick="selectid(0,2);"></p>
<p>本题全选B<input type="checkbox" name="C5" value="ON" onclick="selectid(1,2);"></p>
<p>本题全选<span lang="en">c</span><input type="checkbox" name="C6" value="ON" onclick="selectid(2,2);"><span lang="en"> </span></p></td>
</tr>
</table>
<script>
function selectid(cout,i){
var id = document.getElementById('table1');
var len = id.getElementsByTagName('tr').length;
for(var j=1;j<len-1;j++){

id.rows[j].cells[i].getElementsByTagName('input')[cout].checked = true ;

}
}


</script>



wwq0123 2010-05-27
  • 打赏
  • 举报
回复

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<table border="1" width="100%" id="table1">
<tr>
<td width="112">序号</td>
<td width="158">命名 test1_序号</td>
<td width="148">test2_序号</td>
</tr>
<tr>
<td width="112">1</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_1"> 
B<input type="radio" value="B" name="test1_1">  C<input type="radio" value="C" name="test1_1"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_1"> 
B<input type="radio" value="B" name="test2_1">  C<input type="radio" value="C" name="test2_1"></p>
</td>
</tr>
<tr>
<td width="112">2</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_2"> 
B<input type="radio" value="B" name="test1_2">  C<input type="radio" value="C" name="test1_2"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_2"> 
B<input type="radio" value="B" name="test2_2">  C<input type="radio" value="C" name="test2_2"></p>
</td>
</tr>
<tr>
<td width="112">3</td>
<td width="158"><p>A<input type="radio" value="A" name="test1_3"> 
B<input type="radio" value="B" name="test1_3">  C<input type="radio" value="C" name="test1_3"></p>
</td>
<td width="148"><p>A<input type="radio" value="A" name="test2_3"> 
B<input type="radio" value="B" name="test2_3">  c<input type="radio" value="C" name="test2_3"></p>
</td>
</tr>
<tr>
<td width="112"> </td>
<td width="158">
<p>本题全选A<input type="checkbox" name="C1" value="ON" onclick="selectResult(this,1,'A');"></p>
<p>本题全选B<input type="checkbox" name="C2" value="ON" onclick="selectResult(this,1,'B');"></p>
<p>本题全选C<input type="checkbox" name="C3" value="ON" onclick="selectResult(this,1,'C');"></p>
</td>
<td width="148">
<p>本题全选A<input type="checkbox" name="C4" value="ON" onclick="selectResult(this,2,'A');"></p>
<p>本题全选B<input type="checkbox" name="C5" value="ON" onclick="selectResult(this,2,'B');"></p>
<p>本题全选C<input type="checkbox" name="C6" value="ON" onclick="selectResult(this,2,'C');"></p></td>
</tr>
</table>
<script type="text/javascript">
//index为第几题,r为选择哪个答案
var selectResult=function(c,index,r){
var rad_result = document.getElementsByTagName("input");
for(var i=0;i<rad_result.length;i++){
if(rad_result[i].type.toLowerCase()=="radio"
&&rad_result[i].name.indexOf("test"+index)>=0
&&rad_result[i].value==r){
if(c.checked){
rad_result[i].checked=true;
}
else{
rad_result[i].checked=false;
}
}
}
unselectCheck(c);
}

var unselectCheck=function(c){
var checkboxs=document.getElementsByTagName("input");
for(var i=0;i<checkboxs.length;i++){
if(checkboxs[i].type.toLowerCase()=="checkbox"&&checkboxs[i]!=c){
if(c.checked){
checkboxs[i].checked=false;
}
}
}
}
</script>
</body>
</html>

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧