Posted on 2006-03-08 23:56
大大毛 阅读(617)
评论(0) 编辑 收藏 所属分类:
HTML
这是基于
上一篇
的
实际运用(代码能够实现的特点在上一篇已有表述),这里没有使用多种提交.
源码:
HTML源码
<
HTML
>
<
HEAD
>
<
TITLE
>
Checkbox联动演示--部门/员工
</
TITLE
>
<
script
language
="javascript"
>
//
特定的ID前缀,用于区别页面其它对象
var
myID
=
"
ID
"
;
//
特定的Name前缀,用于区别页面其它对象
var
myName
=
"
Name
"
;
//
页面事件过滤
function
filter()
{
var
Obj
=
window.event.srcElement;
//
过滤条件:
//
.是个Checkbox控件
//
.其类名前缀为指定类名
if
(Obj.type
==
"
checkbox
"
&&
(Obj.className
==
"
dept
"
||
Obj.className
==
"
employee
"
))
{
down(Obj);
up(Obj);
}
else
{
}
}
//
向下处理子控件,使之与当前控件同步
//
theObj:当前处理的控件对象
function
down(theObj)
{
//
得到子控件ID:
//
.ID前缀 + 当前控件有效ID(记录在Name属性中)
var
subObjects;
var
subObjectID;
if
(theObj
!=
null
)
{
subObjectID
=
myID
+
theObj.name.substring(myName.length);
subObjects
=
document.all(subObjectID);
if
(subObjects
!=
null
)
{
if
(subObjects.length)
{
//
一组子控件
for
(
var
i
=
0
;i
<
subObjects.length;i
++
)
{
subObjects[i].checked
=
theObj.checked;
//
向下递归
down(subObjects[i]);
}
}
else
{
//
单个控件
subObjects.checked
=
theObj.checked;
}
}
}
else
{
return
;
}
}
//
向上
//
theObj:当前处理的控件对象
function
up(theObj)
{
var
bortherObj;
var
parentObj;
var
parentObjName;
var
flag
=
0
;
if
((theObj
!=
null
)
&&
(theObj.value
!=
""
))
{
//
得到父控件Name
//
.Name前缀 + 当前控件的ID
parentObjName
=
myName
+
theObj.id.substring(myID.length);
parentObj
=
document.all(parentObjName);
if
(parentObj
!=
null
)
{
bortherObj
=
document.all(theObj.id);
if
(bortherObj.length)
{
//
有平行的兄弟控件,检查条件:
//
.checked值不同
//
.中间有一个的indeterminate状态为真
for
(
var
i
=
0
;i
<
bortherObj.length;i
++
)
{
if
((bortherObj[i].checked
!=
theObj.checked)
||
bortherObj[i].indeterminate
||
theObj.indeterminate)
{
flag
=
1
;
break
;
}
}
if
(flag
==
0
)
{
//
兄弟伙的状态一致,且indeterminate状态为假
parentObj.checked
=
theObj.checked;
parentObj.indeterminate
=
false
;
}
else
{
parentObj.checked
=
true
;
parentObj.indeterminate
=
true
;
}
}
else
{
//
单独一个
parentObj.checked
=
theObj.checked;
parentObj.indeterminate
=
theObj.indeterminate;
}
//
向上递归
up(parentObj);
}
else
{
return
;
}
}
else
{
return
;
}
}
//
得到Checkbox的值
//
.theObj:起始处的对象
//
.result:提交值串
function
getChkValue(theObj,result)
{
var
ID
=
""
;
var
subObjects;
var
subObjectID;
if
(theObj
!=
null
)
{
if
(theObj.indeterminate)
{
//
不确定状态
//
视同于未选择状态
}
else
{
//
记录当前对象
if
(theObj.className
==
"
employee
"
)
{
result
+=
(theObj.checked
?
(
"
,
"
+
theObj.name.substring(myName.length)):
""
);
}
}
//
向下递归
subObjectID
=
myID
+
theObj.name.substring(myName.length);
subObjects
=
document.all(subObjectID);
if
(subObjects
!=
null
)
{
if
(subObjects.length)
{
for
(
var
i
=
0
;i
<
subObjects.length;i
++
)
{
result
=
getChkValue(subObjects[i],result);
}
}
else
{
if
(subObjects.className
==
"
employee
"
)
{
result
+=
(subObjects.checked
?
(
"
,
"
+
subObjects.name.substring(myName.length)):
""
);
}
}
}
}
else
{
return
result;
}
return
result;
}
//
提交用
function
mySubmit()
{
var
result
=
""
;
result
=
getChkValue(document.all(
"
Named01
"
),result);
if
(result
!=
""
)
{
result
=
result.substring(
1
);
}
show(result);
return
false
;
}
//
显示用
function
show(msg)
{
document.all(
"
show
"
).value
=
msg;
}
document.onclick
=
filter;
</
script
>
</
HEAD
>
<
BODY
>
<
table
width
="100%"
cellspacing
="0"
cellpadding
="0"
>
<
tr
height
="60pt"
>
<
td
><
pre
id
="readme"
>
说明
</
pre
></
td
>
</
tr
>
<
tr
height
="60pt"
>
<
td
><
pre
>
Checkbox.class:用于事件过滤,存在有多个class
Checkbox.id: 用于表示层次关系,子对象的ID = 父对象的标识
Checkbox.name: 用于保存对象的标识(唯一标识)
</
pre
></
td
>
</
tr
>
<
tr
>
<
td
><
input
type
="checkbox"
class
="dept"
id
="ID"
name
="Named01"
>
部门d01
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="dept"
id
="IDd01"
name
="Named02"
>
部门d02
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="employee"
id
="IDd02"
name
="Namee01"
>
员工e01
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="dept"
id
="IDd02"
name
="Named05"
>
部门d05
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="employee"
id
="IDd05"
name
="Namee02"
>
员工e02
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="dept"
id
="IDd01"
name
="Named03"
>
部门d03
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="employee"
id
="IDd03"
name
="Namee03"
>
员工e03
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
type
="checkbox"
class
="dept"
id
="IDd01"
name
="Named04"
>
部门d04
</
input
></
td
>
</
tr
>
<
tr
><
td
>
<
input
id
="show"
size
="100"
></
input
></
td
>
</
tr
>
<
tr
><
td
>
<
form
onsubmit
="return mySubmit(); "
>
<
input
type
="submit"
></
input
>
</
form
>
</
td
>
</
tr
>
</
table
>
</
BODY
>
<
script
language
="javascript"
>
var
readme
=
"
<font size=2>
"
;
readme
+=
"
<font size=6>Checkbox联动演示</font> 部门/员工
"
+
"
<br>
"
;
readme
+=
"
作者:大大毛
"
+
"
<br>
"
;
readme
+=
"
修改日期:2006/01/20
"
+
"
<br>
"
;
readme
+=
"
</font>
"
;
document.all.readme.innerHTML
=
readme;
</
script
>
</
HTML
>