javascript 多级checkbox选择效果


Posted in Javascript onAugust 20, 2009

今天总算把部门多选的效果整出来
见图:
javascript 多级checkbox选择效果
先共享核心代码:
1:js脚本

var treeHTML = ""; 
var checkList = new Array(); /*only init here*/ 
var barString = "└";/*┝└*/ 
var degreeString = " "; 
function makeTree(id,text,value,parentid,isCheck) { 
this.id = id; 
this.text = text; 
this.value = value; 
this.parentid = parentid; 
this.isCheck=isCheck; 
} 
function dispCheck(option,degree) { 
for (var i=1;i<=degree;i++) { 
treeHTML += degreeString; 
} 
treeHTML += barString; 
treeHTML += "<input type='checkbox' name='deptOption' onclick='checkOption(this);checkSubOption(this)' id='dept_"+option.value+"' value='"+option.value+"' parentId='"+option.parentid+"' "+ option.isCheck+">"; 
treeHTML += option.text+"</input><br/>"; 
} 
function dispKidsByPid(pid,degree) { 
for (var i=0;i<checkList.length;i++) { 
if (pid==checkList[i].parentid) { 
dispCheck(checkList[i],degree); 
dispKidsByPid(checkList[i].id,degree+1); 
} 
} 
} 
function checkOption(option) 
{ 
var deptCheckList=document.getElementsByName("deptOption"); 
//检查父元素 
if(option.parentId!=0){ 
var parentChecked="0"; 
for(var i=0;i<deptCheckList.length;i++){ 
if(deptCheckList[i].parentId==option.parentId){ 
if(deptCheckList[i].checked){ 
parentChecked="1"; 
break; 
} 
} 
} 
if(parentChecked=="1") 
document.getElementById("dept_"+option.parentId).checked=true; 
else 
document.getElementById("dept_"+option.parentId).checked=false; 
checkOption(document.getElementById("dept_"+option.parentId)); 
} 
} 
function checkSubOption(option){ 
var deptCheckList=document.getElementsByName("deptOption"); 
//检查子元素 
for(var i=0;i<deptCheckList.length;i++){ 
if("dept_"+deptCheckList[i].parentId==option.id){ 
deptCheckList[i].checked=option.checked; 
checkSubOption(deptCheckList[i]); 
} 
} 
}

2:页面:

<%@ page contentType="text/html;charset=GBK"%> 
<%@ page import="java.util.*"%> 
<%@ page import="com.gzdec.eecn.web.school.vo.SchoolRoleVo"%> 
<%@ page import="com.gzdec.common.web.base.BaseAction"%> 
<%@ page import="com.gzdec.common.util.CodeFilter"%> 
<%@ page import="com.gzdec.eecn.web.mas.vo.MasGradeVo" %> 
<%@ page import="com.gzdec.eecn.web.mas.vo.MasSubjectVo" %> 
<%@ page import="com.gzdec.edubase.web.organization.vo.*"%> 
<%@ page import="com.gzdec.eecn.web.school.vo.SchoolRolePrismsVo"%> 
<% 
SchoolRoleVo schoolRoleVo = (SchoolRoleVo) request.getAttribute("schoolRoleVo"); 
List subjecGgroupList = (List) request.getAttribute("subjecGgroupList"); 
List gradeGroupList = (List) request.getAttribute("gradeGroupList"); 
List deptList = (List) request.getAttribute("deptList"); 
List groupList = (List) request.getAttribute("groupList"); 
String roleType=request.getParameter("roleType"); 
SchoolRolePrismsVo schoolRolePrismsVo=(SchoolRolePrismsVo)request.getAttribute("schoolRolePrismsVo"); 
%> 
<script type="text/javascript"> 
<% 
if (deptList!=null) { 
OrgDepartmentVo orgDepartmentVo=new OrgDepartmentVo(); 
String checkList = ""; 
String isCheck=""; 
for (int i=0;i<deptList.size();i++) { 
isCheck=""; 
orgDepartmentVo = (OrgDepartmentVo) deptList.get(i); 
if(schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeDept()!=null&&schoolRolePrismsVo.getRangeDept().indexOf(orgDepartmentVo.getDeptId().toString()+",")>-1) 
isCheck="checked"; 
checkList += "checkList["+i+"]=new makeTree("+orgDepartmentVo.getDeptId()+ 
",'"+orgDepartmentVo.getDeptName()+"',"+orgDepartmentVo.getDeptId()+","+orgDepartmentVo.getParentDept()+",'"+isCheck+"');"; 
} 
out.print(checkList); 
} 
%> 
dispKidsByPid(0,0); 
function showRoleRang(){ 
$('#roleRang').show(); 
} 
function hideRoleRang(){ 
$('#roleRang').hide(1000); 
} 
function showDiv(divId){ 
$('.roleRangOption').each(function(i){ 
if(this.id==divId) 
document.getElementById(this.id).style.display=""; 
else 
document.getElementById(this.id).style.display="none"; 
}); 
}; 
</script> 
<form id="myForm" name="myForm" action="/school/schoolRoleMgr.ee?action=updateSchoolRole" method="post"> 
<input type="hidden" name="roleId" value="<%=schoolRoleVo ==null ?"":schoolRoleVo.getRoleId()%>"/> 
<table align="center" > 
<tr> 
<td align="right">角色名称:</td> 
<td> 
<input type="text" name="roleName" <%="0".equals(roleType)?"readonly":"" %> dataType="LimitB" min="1" max="50" msg="角色名称不能为空,且长度不能大于25个中文字符" class="input_style1" value="<%=schoolRoleVo!=null?CodeFilter.toHtml(schoolRoleVo.getRoleName()!=null?schoolRoleVo.getRoleName():""):""%>"/>  <span style="font-color:red">*</span> 
</td> 
</tr> 
<tr> 
<td align="right">角色描述:</td> 
<td> 
<textarea name="roleDesc" <%="0".equals(roleType)?"readonly":"" %> rows="5" cols="50" require="false" datatype="Limit" msg="角色描述不大于128个中文字符" max="255"><%=schoolRoleVo!=null?CodeFilter.toHtml(schoolRoleVo.getRoleDesc()!=null?schoolRoleVo.getRoleDesc():""):""%></textarea> 
</td> 
</tr> 
<tr> 
<td align="right">配发短信数:</td> 
<td> 
<input type="text" name="totalNum" require="false" datatype="Number" msg="配发短信数必须为数字" class="input_style1" value="<%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getSmsTotalNum()!=null?schoolRolePrismsVo.getSmsTotalNum().toString():""%>"/> 
条 现在还剩<%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getSmsTotalNum()!=null?schoolRolePrismsVo.getSmsTotalNum().longValue()-schoolRolePrismsVo.getSmsSendNum().longValue():"0"%>条可配送 
</td> 
</tr> 
<tr> 
<td align="right">可发范围:</td> 
<td> 
<input type="button" value="选择" onclick="showRoleRang()"/> 
<input type="button" value="确定" onclick="hideRoleRang()"/> 
</td> 
</tr> 
<tr> 
<td align="right"></td> 
<td> 
<div id="roleRang" style="display:none"> 
<div class="basic" id="list1a"> 
<a onclick="showDiv('div1')">系统选项</a> 
<div style="display:none" id="div1" class="roleRangOption"> 
<p> 
<input type="checkbox" name="sysOption" value="JS" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeSystem()!=null&&schoolRolePrismsVo.getRangeSystem().indexOf("JS,")!=-1?"checked":"" %>>全校教师<br/> 
<input type="checkbox" name="sysOption" value="BZR" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeSystem()!=null&&schoolRolePrismsVo.getRangeSystem().indexOf("BZR,")!=-1?"checked":"" %>>全校班主任<br/> 
<input type="checkbox" name="sysOption" value="XS" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeSystem()!=null&&schoolRolePrismsVo.getRangeSystem().indexOf("XS,")!=-1?"checked":"" %>>全校学生<br/> 
<input type="checkbox" name="sysOption" value="JFRY" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeSystem()!=null&&schoolRolePrismsVo.getRangeSystem().indexOf("JFRY,")!=-1?"checked":"" %>>全校教辅人员<br/> 
</p> 
</div> 
<a onclick="showDiv('div2')">行政部门</a> 
<div style="display:none" id="div2" class="roleRangOption"> 
<p id="deptInfo" style="height:100px;overflow :scroll"> 
</p> 
</div> 
<a onclick="showDiv('div3')">学科分组</a> 
<div style="display:none" id="div3" class="roleRangOption"> 
<p style="height:100px;overflow :scroll"> 
<% 
if(subjecGgroupList!=null&&!subjecGgroupList.isEmpty()){ 
for(int i=0;i<subjecGgroupList.size();i++){ 
MasSubjectVo masSubjectVo = (MasSubjectVo) subjecGgroupList.get(i); 
%> 
<input type="checkbox" name="subjectOption" value="<%=masSubjectVo.getSubjectId() %>" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeSubject()!=null&&schoolRolePrismsVo.getRangeSubject().indexOf(masSubjectVo.getSubjectId().toString()+",")!=-1?"checked":"" %>><%=masSubjectVo.getName() %><br/> 
<% 
} 
} 
%> 
</p> 
</div> 
<a onclick="showDiv('div4')">年级分组</a> 
<div style="display:none" id="div4" class="roleRangOption"> 
<p> 
<% 
if(gradeGroupList!=null&&!gradeGroupList.isEmpty()){ 
for(int i=0;i<gradeGroupList.size();i++){ 
MasGradeVo masGradeVo = (MasGradeVo) gradeGroupList.get(i); 
%> 
<input type="checkbox" name="gradeOption" value="<%=masGradeVo.getGradeCode() %>" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeGrade()!=null&&schoolRolePrismsVo.getRangeGrade().indexOf(masGradeVo.getGradeCode().toString()+",")!=-1?"checked":"" %>><%=masGradeVo.getName() %><br/> 
<% 
} 
} 
%> 
</p> 
</div> 
<a onclick="showDiv('div5')">校内分组</a> 
<div style="display:none" id="div5" class="roleRangOption"> 
<p style="height:100px"> 
<% 
if(groupList!=null&&!groupList.isEmpty()){ 
for(int i=0;i<groupList.size();i++){ 
OrgBgroupVo orgBgroupVo=(OrgBgroupVo)groupList.get(i); 
%> 
<input type="checkbox" name="groupOption" value="<%=orgBgroupVo.getBId() %>" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeInterest()!=null&&schoolRolePrismsVo.getRangeInterest().indexOf(orgBgroupVo.getBId().toString()+",")!=-1?"checked":"" %>><%=orgBgroupVo.getBName()%><br/> 
<% 
} 
} 
%> 
</p> 
</div> 
<a onclick="showDiv('div6')">学生年级</a> 
<div style="display:none" id="div6" class="roleRangOption"> 
<p> 
<% 
if(gradeGroupList!=null&&!gradeGroupList.isEmpty()){ 
for(int i=0;i<gradeGroupList.size();i++){ 
MasGradeVo masGradeVo = (MasGradeVo) gradeGroupList.get(i); 
%> 
<input type="checkbox" name="studentOption" value="<%=masGradeVo.getGradeCode() %>" <%=schoolRolePrismsVo!=null&&schoolRolePrismsVo.getRangeStuGrade()!=null&&schoolRolePrismsVo.getRangeStuGrade().indexOf(masGradeVo.getGradeCode().toString()+",")!=-1?"checked":"" %>><%=masGradeVo.getName() %><br/> 
<% 
} 
} 
%> 
</p> 
</div> 
</div> 
</td> 
</tr> 
<tr> 
<td align="right"> </td> 
<td valign="top"><span style="display:none" id="message"></span></td> 
</tr> 
</table> 
</form>

3:css
li { list-style-type: none; } 
.basic { width:20em; } 
.basic { 
width: 260px; 
font-family: verdana; 
border: 1px solid black; 
} 
.basic div { 
background-color: #eee; 
} 
.basic p { 
margin-bottom : 10px; 
border: none; 
text-decoration: none; 
font-weight: bold; 
font-size: 10px; 
margin: 0px; 
padding: 10px; 
} 
.basic a { 
cursor:pointer; 
display:block; 
padding:5px; 
margin-top: 0; 
text-decoration: none; 
font-weight: bold; 
font-size: 12px; 
color: black; 
background-color: #00a0c6; 
border-top: 1px solid #FFFFFF; 
border-bottom: 1px solid #999; 
background-image: url("AccordionTab0.gif"); 
} 
.basic a:hover { 
background-color: white; 
background-image: url("AccordionTab2.gif"); 
} 
.basic a.selected { 
color: black; 
background-color: #80cfe2; 
background-image: url("AccordionTab2.gif"); 
}
Javascript 相关文章推荐
再谈IE中Flash控件的自动激活 ObjectWrap
Mar 09 Javascript
js类中获取外部函数名的方法
Aug 19 Javascript
利用百度地图JSAPI生成h7n9禽流感分布图实现代码
Apr 15 Javascript
Node.js实现批量去除BOM文件头
Dec 20 Javascript
jquery获取当前元素索引值用法实例
Jun 10 Javascript
基于AngularJS实现iOS8自带的计算器
Sep 12 Javascript
Bootstrap模态框调用功能实现方法
Sep 19 Javascript
ES6中Math对象的部分扩展
Feb 20 Javascript
详解vue-cli与webpack结合如何处理静态资源
Sep 19 Javascript
vue-cli项目优化方法- 缩短首屏加载时间
Apr 01 Javascript
vue基础之data存储数据及v-for循环用法示例
Mar 08 Javascript
three.js利用gpu选取物体并计算交点位置的方法示例
Nov 25 Javascript
调用js时ie6和ie7,ff的区别
Aug 19 #Javascript
jquery 入门教程 [翻译] 推荐
Aug 17 #Javascript
jQuery select控制插件
Aug 17 #Javascript
javascript 无提示关闭窗口脚本
Aug 17 #Javascript
javascript addBookmark 加入收藏 多浏览器兼容
Aug 15 #Javascript
用JavaScript将从数据库中读取出来的日期型格式化为想要的类型。
Aug 15 #Javascript
javascript 客户端验证上传图片的大小(兼容IE和火狐)
Aug 15 #Javascript
You might like
为什么《星际争霸》是测试人工智能的理想战场
2019/12/03 星际争霸
php smarty模版引擎中变量操作符及使用方法
2009/12/11 PHP
CI框架中zip类应用示例
2014/06/17 PHP
ThinkPHP3.1新特性之Action参数绑定
2014/06/19 PHP
脚本之家贴图转换+转贴工具用到的js代码超级推荐
2007/04/05 Javascript
javascript 常用关键字列表集合
2007/12/04 Javascript
range 标准化之获取
2011/08/28 Javascript
json数据与字符串的相互转化示例
2013/09/18 Javascript
jquery选择器简述
2015/08/31 Javascript
详解javascript事件冒泡
2016/01/09 Javascript
JavaScript实现DOM对象选择器
2016/09/24 Javascript
浅析jQuery操作select控件的取值和设值
2016/12/07 Javascript
vue  directive定义全局和局部指令及指令简写
2018/11/20 Javascript
如何实现双向绑定mvvm的原理实现
2019/05/28 Javascript
实现vuex与组件data之间的数据同步更新方式
2019/11/12 Javascript
原生Vue 实现右键菜单组件功能
2019/12/16 Javascript
JavaScript实现省份城市的三级联动
2020/02/11 Javascript
微信小程序连续签到7天积分获得功能的示例代码
2020/08/20 Javascript
Vue实现boradcast和dispatch的示例
2020/11/13 Javascript
vscode自定义vue模板的实现
2021/01/27 Vue.js
[49:20]VG vs TNC Supermajor小组赛B组败者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
[00:43]魔廷新尊——痛苦女王至宝捆绑包
2020/06/12 DOTA
Python实现身份证号码解析
2015/09/01 Python
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
2017/07/20 Python
Python3实现将本地JSON大数据文件写入MySQL数据库的方法
2018/06/13 Python
在python中计算ssim的方法(与Matlab结果一致)
2019/12/19 Python
SpringBoot实现登录注册常见问题解决方案
2020/03/04 Python
求职简历中个人的自我评价
2013/12/25 职场文书
澳大利亚商务邀请函
2014/01/17 职场文书
物业消防安全责任书
2014/07/23 职场文书
2014年小学美术工作总结
2014/12/20 职场文书
办公室禁烟通知
2015/04/23 职场文书
交心谈心活动总结
2015/05/11 职场文书
2016秋季幼儿园开学寄语
2015/12/03 职场文书
一文搞懂python异常处理、模块与包
2021/06/26 Python
解析python中的jsonpath 提取器
2022/01/18 Python