jquery 的checkbox 操作1

来源:互联网 发布:知豆电动汽车电池寿命 编辑:程序博客网 时间:2024/06/07 06:38
<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
    <title></title>
    <scriptsrc="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
    <scripttype="text/javascript">
        $(function () {
            // 全选
            $("#btnCheckAll").bind("click", function () {
                $("[name = chkItem]:checkbox").attr("checked", true);
            });
 
            // 全不选
            $("#btnCheckNone").bind("click", function () {
                $("[name = chkItem]:checkbox").attr("checked", false);
            });
 
            // 反选
            $("#btnCheckReverse").bind("click", function () {
                $("[name = chkItem]:checkbox").each(function () {
                    $(this).attr("checked", !$(this).attr("checked"));
                });
            });
 
            // 全不选
            $("#btnSubmit").bind("click", function () {
                var result = new Array();
                $("[name = chkItem]:checkbox").each(function () {
                    if ($(this).is(":checked")) {
                        result.push($(this).attr("value"));
                    }
                });
 
                alert(result.join(","));
            });
        });
    </script>
</head>
<body>
    <div>
        <inputname="chkItem" type="checkbox" value="今日话题" />今日话题
        <inputname="chkItem" type="checkbox" value="视觉焦点" />视觉焦点
        <inputname="chkItem" type="checkbox" value="财经" />财经
        <inputname="chkItem" type="checkbox" value="汽车" />汽车
        <inputname="chkItem" type="checkbox" value="科技" />科技
        <inputname="chkItem" type="checkbox" value="房产" />房产
        <inputname="chkItem" type="checkbox" value="旅游" />旅游
    </div>
    <div>
        <inputid="btnCheckAll" type="button" value="全选" />
        <inputid="btnCheckNone" type="button" value="全不选" />
        <inputid="btnCheckReverse" type="button" value="反选" />
        <inputid="btnSubmit" type="button" value="提交" />
    </div>
</body>
</html>
复制代码

2、checkbox table选中

效果图:

代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
    <title></title>
    <styletype="text/css">
        table
        {
            border-collapse: collapse;
        }
        td
        {
            border: 1px solid #ccc;
        }
    </style>
    <scriptsrc="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
    <scripttype="text/javascript">
        $(function () {
            // chkAll全选事件
            $("#chkAll").bind("click", function () {
                $("[name = chkItem]:checkbox").attr("checked", this.checked);
            });
 
            // chkItem事件
            $("[name = chkItem]:checkbox").bind("click", function () {
                var $chk = $("[name = chkItem]:checkbox");
                $("#chkAll").attr("checked", $chk.length == $chk.filter(":checked").length);
            })
        });
    </script>
</head>
<body>
    <tableid="tb">
        <thead>
            <tr>
                <td>
                    <inputid="chkAll" type="checkbox" />
                </td>
                <td>
                    分类名称
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="今日话题" />
                </td>
                <td>
                    今日话题
                </td>
            </tr>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="视觉焦点" />
                </td>
                <td>
                    视觉焦点
                </td>
            </tr>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="财经" />
                </td>
                <td>
                    财经
                </td>
            </tr>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="汽车" />
                </td>
                <td>
                    汽车
                </td>
            </tr>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="科技" />
                </td>
                <td>
                    科技
                </td>
            </tr>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="房产" />
                </td>
                <td>
                    房产
                </td>
            </tr>
            <tr>
                <td>
                    <inputname="chkItem" type="checkbox" value="旅游" />
                </td>
                <td>
                    旅游
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>
0 0
原创粉丝点击