我有10个左右的checkbox ,在一个form里面,分为两组,第一个name 为checkbox01,第 2345 name为checkbox[],当第一个checkbox选中的时候2345选中;同样,第六个checkbox name 为checkbox02,第 7、8、9、10 name为checkbox[],当第六个checkbox选中的时候7、8、9、10也选中,javascript怎么写?
---------------------------------------------------------------
1<body>
2<table border="1" width="100%">
3<tr>
4<td width="50%">
5<script>
6function chkall1(all){
7for(i=0;i<form1.chkboxlist.length;i++)
8form1.chkboxlist[i].checked=all;
9
10}
11</script>
12<form action="" method="post" name="form1">
13<input name="allbox" onclick="chkall1(this.checked);" type="checkbox" value="Check All"/>全选<br/>
14<input name="chkboxlist" type="checkbox"/>信息1-1<br/>
15<input name="chkboxlist" type="checkbox"/>信息1-2<br/>
16<input name="chkboxlist" type="checkbox"/>信息1-3<br/>
17<input name="chkboxlist" type="checkbox"/>信息1-4
18<p>
19</p></form>
20</td>
21<td width="50%">
22<script>
23function chkall2(all){
24for(i=0;i<form2.chkboxlist.length;i++)
25form2.chkboxlist[i].checked=all;
26
27}
28</script>
29<form action="" method="post" name="form2">
30<input name="allbox" onclick="chkall2(this.checked);" type="checkbox" value="Check All"/>全选<br/>
31<input name="chkboxlist" type="checkbox"/>信息2-1<br/>
32<input name="chkboxlist" type="checkbox"/>信息2-2<br/>
33<input name="chkboxlist" type="checkbox"/>信息2-3<br/>
34<input name="chkboxlist" type="checkbox"/>信息2-4<br/>
35<p>
36</p></form>
37</td>
38</tr>
39</table>
40</body>
---------------------------------------------------------------
1<script>
2function ch1()
3{
4for(var i=0;i<document.f.checkbox01A.length;i++)
5{
6document.f.checkbox01A[i].checked=true
7}
8}
9function ch2()
10{
11for(var i=0;i<document.f.checkbox02A.length;i++)
12{
13document.f.checkbox02A[i].checked=true
14}
15}
16</script>
1<form action="aaa.asp" name="f">
2<input name="checkbox01" onclick="ch1()" type="checkbox"/>
3<input name="checkbox01A" type="checkbox"/>
4<input name="checkbox01A" type="checkbox"/>
5<input name="checkbox01A" type="checkbox"/>
6<input name="checkbox01A" type="checkbox"/>
7<br/>
8<input name="checkbox02" onclick="ch2()" type="checkbox"/>
9<input name="checkbox02A" type="checkbox"/>
10<input name="checkbox02A" type="checkbox"/>
11<input name="checkbox02A" type="checkbox"/>
12<input name="checkbox02A" type="checkbox"/>
13</form>
---------------------------------------------------------------
我在net_lover(孟子E章)的基础上改了一下。使之选之全选,不选全不选。
1<script>
2function ch1()
3{
4if (document.f.checkbox01.checked)
5for(var i=0;i<document.f.checkbox01A.length;i++)
6document.f.checkbox01A[i].checked=true
7else
8for(var i=0;i<document.f.checkbox01A.length;i++)
9document.f.checkbox01A[i].checked=false
10}
11function ch2()
12{
13if (document.f.checkbox02.checked)
14for(var i=0;i<document.f.checkbox02A.length;i++)
15document.f.checkbox02A[i].checked=true
16else
17for(var i=0;i<document.f.checkbox02A.length;i++)
18document.f.checkbox02A[i].checked=false
19}
20</script>
1<form action="aaa.asp" name="f">
2<input name="checkbox01" onclick="ch1()" type="checkbox"/>
3<input name="checkbox01A" type="checkbox"/>
4<input name="checkbox01A" type="checkbox"/>
5<input name="checkbox01A" type="checkbox"/>
6<input name="checkbox01A" type="checkbox"/>
7<br/>
8<input name="checkbox02" onclick="ch2()" type="checkbox"/>
9<input name="checkbox02A" type="checkbox"/>
10<input name="checkbox02A" type="checkbox"/>
11<input name="checkbox02A" type="checkbox"/>
12<input name="checkbox02A" type="checkbox"/>
13</form>
Top
---------------------------------------------------------------
1<script>
2function ch(flag){
3var i=0;while(1)try{document.all(event.srcElement.name+"A")[i++].checked=event.srcElement.checked}catch(e){break}
4}
5</script>
1<form action="aaa.asp" name="f">
2<input name="checkbox01" onclick="ch()" type="checkbox"/>
3<input name="checkbox01A" type="checkbox"/>
4<input name="checkbox01A" type="checkbox"/>
5<input name="checkbox01A" type="checkbox"/>
6<input name="checkbox01A" type="checkbox"/>
7<br/>
8<input name="checkbox02" onclick="ch()" type="checkbox"/>
9<input name="checkbox02A" type="checkbox"/>
10<input name="checkbox02A" type="checkbox"/>
11<input name="checkbox02A" type="checkbox"/>
12<input name="checkbox02A" type="checkbox"/>
13</form>
---------------------------------------------------------------
checkbox[]是非法名字
---------------------------------------------------------------
checkbox[]???
这个是php的用法吧,在这里是非法的!
---------------------------------------------------------------
不对,如果你用php,这种用法是正确,我到是不清楚你出了什么问题?
这个是正确用法,被孟子误导了!
---------------------------------------------------------------
1<script>
2function ch1(obj)
3{
4for(var i=0;i<document.f.elements["checkbox[]"].length/2;i++)
5{
6document.f.elements["checkbox[]"][i].checked=obj.checked
7}
8}
9function ch2(obj)
10{
11for(var i=document.f.elements["checkbox[]"].length/2;i<document.f.elements["checkbox[]"].length;i++)
12{
13document.f.elements["checkbox[]"][i].checked=obj.checked
14}
15}
16</script>
1<form action="aaa.asp" name="f">
2<input name="checkbox01" onclick="ch1(this)" type="checkbox"/>全选:
3<input name="checkbox[]" type="checkbox"/>
4<input name="checkbox[]" type="checkbox"/>
5<input name="checkbox[]" type="checkbox"/>
6<input name="checkbox[]" type="checkbox"/>
7<br/>
8<input name="checkbox02" onclick="ch2(this)" type="checkbox"/>全选:
9<input name="checkbox[]" type="checkbox"/>
10<input name="checkbox[]" type="checkbox"/>
11<input name="checkbox[]" type="checkbox"/>
12<input name="checkbox[]" type="checkbox"/>
13</form>
---------------------------------------------------------------
Nothing Is Impossible In JavaScript Column!
:)