javascript操作checkbox

来源:互联网 发布:企业防泄密软件 编辑:程序博客网 时间:2024/05/22 00:22

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="../taglib.jsp" %>

<html>
<head>
 <title>功能分配</title>
 <link href="PatentAid/StyleCss/Style.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript">
 
  function toChecked(str)
  {
   var a = document.getElementsByName("menus");
   var n = a.length;
   for(var i=0;i<n;i++)
   { 
<%--   若当前checkbox的父项未选中,则选中--%>
    if(a[i].value==str.substring(0,2)){
     a[i].checked = true;
    }
<%--   选中顶级checkbox--%>
    if(a[i].value=="00"){
     a[i].checked = true;
    }
   }
  }
<%-- 如果事件源的名字是menus则根据选择的项进行处理--%>
  function change()
  {
   if('menus'==event.srcElement.name)
   {
<%--   如果选择是非根目录,若上级checkbox未选中,由以下代码处理选中--%>
    if(event.srcElement.value.length>=2){
     if(event.srcElement.checked==true)
     {
      toChecked(event.srcElement.value);
     }
    }
   }
  }
 </script>
</head>

<body>

    <div style={border:1;width=290;height:400} onclick="change()">
     <menu:useMenuDisplayer repository="repository" name="MenuForm" bundle="org.apache.struts.action.MESSAGE">
   <menu:displayMenu name="00" />
  </menu:useMenuDisplayer>
    </div>
   
 <div align="right">
  <table>
    <tr align="center" valign="middle">
     <td><input name="hdnValue" type="hidden" id="hdnValue" style="width : 10px" />
         <input name="hdnText" type="hidden" id="hdnText" style="width : 10px" />
         <input type="submit" name="btnSave" value="确 定" id="btnSave" class="button-normal" />
     </td>
     <td>
      <input id="BtnCancel" type="button" value="关 闭" onclick="javascript:window.returnValue=false;window.close();" class="button-normal">
                 </td>
    </tr>
  </table>
 </div>
</body>
</html>