关于CHECKBOX如何分类选择的问题,高分求救,高手帮帮忙啊~~
要从表user里取出人员姓名,并按照部门分类列出来,每个姓名前面都带了一个checkbox,然后每列底部有一个这个部门的checkbox,如下:
口aaa 口abc 口bca
口bbb 口acb 口bbc
口ccc 口acb 口bac
口行政 口采购 口财务
口全体员工
现在要求:勾选各个部门的时候选择该部门所有员工,勾选全体员工的时候选择全部员工,现有一个user表,字段:id,name,dept ,怎么样完成上述要求的功能呢?
------解决方案--------------------每个checkbox加个属性如
deptid= <%=deptid%>
部门的id为它的id
选择某个部门的时候做一个循环
function DeptClick(obj)
{
var checks = document.all.tags( "input ");
for(var i=0;i <checks.length;i++)
{
if(checks[i].type == "checkbox " && checks[i].deptid = obj.id)
{
checks[i].checked = obj.checked
}
}
}
------解决方案-------------------- <script>
function chkworkerall(){
var obj = document.forms[0];
if (obj.worker.length){
for (i = 0; i < obj.worker.length; i++){
obj.worker[i].checked = obj.workerall.checked;
}
}else{
obj.worker.checked = obj.workerall.checked;
}
}
function chkdept(obj1){
var obj = document.forms[0];
if (obj.worker.length){
for (i = 0; i < obj.worker.length; i++){
if (obj.worker[i].getAttribute( "dept ") == obj1.value){
obj.worker[i].checked = obj1.checked;
}
}
}else{
if (obj.worker.getAttribute( "dept ") == obj1.value) obj.worker.checked = obj.workerall.checked;
}
}
</script>
<form>
以下循环人员:
<input type= "checkbox " name= "worker " dept= "1 " value= "1 " /> worker1
<input type= "checkbox " name= "worker " dept= "2 " value= "2 " /> worker2
<input type= "checkbox " name= "worker " dept= "2 " value= "3 " /> worker3 <br>
以下循环部门:
<input type= "checkbox " name= "dept " value= "1 " onclick= "chkdept(this); " /> dept1
<input type= "checkbox " name= "dept " value= "2 " onclick= "chkdept(this); " /> dept2 <br>
以下控制所有人:
<input type= "checkbox " name= "workerall " onclick= "chkworkerall(); " /> all
</form>