jQuery判断是否被点击并遍历显示被点击的CheckBox

效果预览

 

 HTML代码

<div>效果预览</div>
    <input id="CheckBox1" type="checkbox" name="chkbox" value="a" /><label for="CheckBox1">a</label>
    <input id="CheckBox2" type="checkbox" name="chkbox" value="b" /><label for="CheckBox2">b</label>
    <input id="CheckBox3" type="checkbox" name="chkbox" value="c" /><label for="CheckBox3">c</label>
    <p id="msg"></p>

JavaScript代码

    <script type="text/javascript">
        $(function () {
            //new test().a();
            $("input:[name=chkbox]").click(function () {
                var arr = new Array();
                $("input:[name=chkbox]:checked").each(function (key, value) {
                    arr[key] = $(value).val();
                });
                $("#msg").text("您选择了:" + arr.length + "项" + arr.join(","));
            });
        });
        function test() {
            this.a = function () {
                alert("hello");
            };
        };
    </script>

 

posted @ 2012-09-30 11:56  寂静之秋  阅读(447)  评论(0编辑  收藏  举报
哈尔滨八零网