再很多网站里这都是一个很常见的效果,就是
点击总分类的复选框,自动选择所有子分类的复选框,或者如果子分类的所有复选框全部被选中则总分类复选框也自动被选中。
这次偶时再msn的网站上偶然看到的,于是自己也试着写了下,代码如下:
<!
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"
xml:lang
="en"
lang
="en"
>
<
head
>
<
title
></
title
>
<
script
type
="text/javascript"
>
function
checkAll(str){
var
a
=
document.getElementById(
"
t
"
+
str).getElementsByTagName(
"
input
"
);
var
b
=
document.getElementById(
"
d
"
+
str);
for
(
var
i
=
0
;i
<
a.length;i
++
){ a[i].checked
=
b.checked; } }
function
fixChk(str){
var
a
=
document.getElementById(
"
t1
"
).getElementsByTagName(
"
input
"
);
var
b
=
document.getElementById(
"
d1
"
);
for
(
var
i
=
0
;i
<
a.length;i
++
){
if
(a[i].checked
==
false
){ b.checked
=
false
;
return
; } } b.checked
=
true
; }
</
script
>
</
head
>
<
body
>
<
center
>
<
div
style
="width:90%;text-align:left;border:1px solid #ccc;height:25px;line-height:25px"
><
input
type
="checkbox"
id
="d1"
onclick
="checkAll('1')"
/>
休闲娱乐
</
div
>
<
div
style
="width:90%;text-align:left;border:1px solid #ccc;"
id
="t1"
>
<
input
type
="checkbox"
onclick
="fixChk('1')"
/>
书籍及杂志
<
input
type
="checkbox"
onclick
="fixChk('1')"
/>
音乐及影碟
<
br
/>
<
input
type
="checkbox"
onclick
="fixChk('1')"
/>
旅游
</
div
>
</
center
>
</
body
>
</
html
>
转载请注明原文地址: https://ibbs.8miu.com/read-800078.html