首页 > 编程 > JavaScript > 正文

JS小功能(checkbox实现全选和全取消)实例代码

2019-11-20 21:33:59
字体:
来源:转载
供稿:网友

效果:

代码:

复制代码 代码如下:

<head runat="server">
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var ckall = document.getElementById('ck1');
            var ck = document.getElementsByName('name1');
            ckall.onclick = function () {
                for (var i = 0; i < ck.length; i++) {
                    if (ckall.checked == true) {
                        ck[i].checked = true;
                    }
                    else {
                        ck[i].checked = false;
                    }
                }
            }
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <input type="checkbox" id="ck1" />
    全选
    <div id="div1">
        <input type="checkbox" name="name1" />
        <input type="checkbox" name="name1" />
        <input type="checkbox" name="name1" />
        <input type="checkbox" name="name1" />
        <input type="checkbox" name="name1" />
        <input type="checkbox" name="name1" />
    </div>
    </form>
</body>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表