checkbox全选,全不选的应用

<html>
<head>
    <script>
        function checkEvent(name, allCheckId) {
            var allCk = document.getElementById(allCheckId);
            if (allCk.checked == true)
                checkedarry = checkAll(name);
            else
                uncheckedarry = checkAllNo(name);

        }

        function checkAll(name) {
            var names = document.getElementsByName(name);
            var checkedarry = new Array();
            var len = names.length;
            if (len > 0) {
                var i = 0;
                for (i = 0; i < len; i++) {
                    names[i].checked = true;
                    checkedarry[i] = names[i].value;
                }
            }
            return checkedarry;
        }

        //全不选
        function checkAllNo(name) {
            var names = document.getElementsByName(name);
            var uncheckedarry = new Array();
            var len = names.length;
            if (len > 0) {
                var i = 0;
                for (i = 0; i < len; i++) {
                    names[i].checked = false;
                    uncheckedarry[i] = names[i].value;
                }
            }
            return uncheckedarry;
        }

        /** 
        * 复选框变化  全选按钮变化 
        **/
        function toChkSon(ckname, obj) {
        
            if (obj.checked == false) //当此复选框未选中 全选为未选 
            {
                document.getElementById(ckname).checked = false;
                return;
            }
  
            var chkInputs = getCheckBox(obj); //获取所有复选框 
            var j = 0;
            for (var i = 0; i < chkInputs.length; i++) {
                if (chkInputs[i].checked == obj.checked)
                    j++;
                else
                    break;
            }
    
            if (j == chkInputs.length) //当所有复选框为同一状态时 赋值全选同一状态 
                document.getElementById(ckname).checked = obj.checked;
        }

        /** 
        * 获取所有复选框 
        **/
        function getCheckBox(obj) {
            var inputs = document.getElementsByTagName("input");
       
            var chkInputs = new Array();
            var j = 0;
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].type == "checkbox" && inputs[i].id == obj.id) //刷选出所有复选框 
                {
                    chkInputs[j] = inputs[i];
                    j++;
                }
            }
  
            return chkInputs;
        }

    </script>
</head>
<body>
    <form id="forms" name="forms">
    <input id="HiddenCount" type="hidden" value="39" /><table border='0' cellpadding="2"
        cellspacing="1" bgcolor="#BADCF1">
        <tr bgcolor="#FFFFFF">
            <td>
                <input type="checkbox" id="ckall0" onclick="checkEvent('ck0','ckall0')" />部门管理
            </td>
            <td>
                <input type="checkbox" id="ck0" name="ck0" onclick="toChkSon('ckall0',this)"
                    value="04cef96723e941f1896220f1e5523123" checked="checked" />添加部门
                <input type="checkbox" id="ck0" name="ck0" onclick="toChkSon('ckall0',this)"
                    value="1249002215514aa1b11e7f136691ebba" checked="checked" />删除部门
                <input type="checkbox" id="ck0" name="ck0" onclick="toChkSon('ckall0',this)"
                    value="56779872760e474eb6bf33b8f52f38a1" checked="checked" />修改部门
                <input type="checkbox" id="ck0" name="ck0" onclick="toChkSon('ckall0',this)"
                    value="689832fc0bf946efb6a85030f6ba807f" checked="checked" />部门管理
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <input type="checkbox" id="ckall1" onclick="checkEvent('ck1','ckall1')" />财务收入管理
            </td>
            <td>
                <input type="checkbox" id="ck1" name="ck1" onclick="toChkSon('ckall1',this)"
                    value="724b685a277e4df1a9052ce72ca7a72f" checked="checked" />财务收入管理
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

你可能感兴趣的:(function,input,border,Forms)