永久的CheckBox(单选,全选/反选)!

<html>
<head>
    <title>选择</title>
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //单选组事件
            $("[name=chk]:checkbox").bind("click", function () {
                if ($(this).attr("check")) {
                    $(this).removeAttr("check");
                } else {
                    $(this).attr("check", true);
                }
            })

            //全选或反选
            $("[name=chkName]:checkbox").click(function () {
                if ($(this).attr("check")) {
                    $(this).removeAttr("check");
                    $("[name=chk]:checkbox").each(function () {
                        if ($(this).attr("check")) {
                            $(this).click();
                        };
                    });
                } else {
                    $(this).attr("check", true);
                    $("[name=chk]:checkbox").each(function () {
                        if (!$(this).attr("check")) {
                            $(this).click();
                        };
                    });
                }

            })


            $("#btnSub").click(function () {
                var arr = [];
                $("[name=chk]:checkbox").each(function () {
                    if ($(this).attr("check")) {
                        arr[arr.length] = $(this).attr("value");
                    }
                })
                alert(arr.join(","))
            })
        })
    </script>
</head>
<body>
    <div>
        <div>
            <span>
                <input type="checkbox" id="chkAll" name="chkName" /></span>
        </div>
        <div>
            <input type="checkbox" id="Checkbox1" name="chk" value="1" />
            <input type="checkbox" id="Checkbox2" name="chk" value="2" />
            <input type="checkbox" id="Checkbox3" name="chk" value="3" />
            <input type="checkbox" id="Checkbox4" name="chk" value="4" />
        </div>
        <div>
            <input type="button" id="btnSub" value="提交" />
        </div>
    </div>
</body>
</html>
在寂寞的日子里沉淀自己,在程序的日子里找到自己,我为梦想而坚持!

如果对你有重要帮助,可以打赏一下!


原文地址:https://www.cnblogs.com/Kummy/p/3163504.html