jQuery中擷取checkbox選中項等操作及注意事項

來源:互聯網
上載者:User

1. 擷取checkbox的選中項
2. checkbox選項的全選 反選操作

用於測試的checkbox程式碼片段:

複製代碼 代碼如下:
<div>
            <input type="checkbox" name="abc" value="一年級" id="in1" checked="checked" /><label for="in1">一年級</label>
            <input type="checkbox" name="abc" value="二年級" id="in2" /><label for="in2">二年級</label>
            <input type="checkbox" name="abc" value="三年級" id="in3" /><label for="in3">三年級</label>
            <input type="checkbox" name="abc" value="四年級" id="in4" /><label for="in4">四年級</label>
            <input type="checkbox" name="abc" value="五年級" id="in5" /><label for="in5">五年級</label>
            <input type="checkbox" name="abc" value="六年級" id="in6" /><label for="in6">六年級</label>
            <input type="checkbox" name="abc" value="七年級" id="in7" /><label for="in7">七年級</label>
            <input type="checkbox" name="abc" value="八年級" id="in8" /><label for="in8">八年級</label>
        </div>


一:首先來說第一點,擷取checkbox的選中項。網上搜到的大部分方法使用each來擷取:

複製代碼 代碼如下:
$("input[name='checkbox'][checked]").each(function () {
    alert(this.value);
})

但在測試時我就遇到了問題,這種方法在IE下可以擷取,但在firefox和chrome瀏覽器下就無法擷取當前的選中項,測試效果如下:

IE下的測試效果(我的是IE10):

IE10下的效果:

chrome瀏覽器下的效果:

通過在google上搜尋,找到了原因:

網址:  Jquery 選中多少個input CheckBox問題,IE正常,FF和Chrome無法取到值

因為我用的jquery版本是1.7.2的,所以這裡我得用 :checked 來擷取才行,修改後的代碼:

複製代碼 代碼如下:
//擷取選中項
            $('#huoqu2').click(function () {
                $('#show').html("");
                $("input[name='abc']:checked").each(function () {
                    //alert(this.value);
                    $('#show').append(this.value + "  ");
                });
            });

在chrome下的效果:

二:checkbox的全選 反選操作:

由於這兩個比較簡單,我就直接上代碼吧:

複製代碼 代碼如下:
//全選/取消全選
            $('#quanxuan').toggle(function () {
                $("input[name='abc']").attr("checked", 'true');
            }, function () {
                $("input[name='abc']").removeAttr("checked");
            });


            //反選
            $('#fanxuan').click(function () {
                $("input[name='abc']").each(function () {
                    if ($(this).attr("checked")) {
                        $(this).removeAttr("checked");
                    } else {
                        $(this).attr("checked", 'true');
                    }
                });
            });

再總結一下:

jquery版本在1.3之前時,擷取checkbox的選中項的操作:

複製代碼 代碼如下:
                $("input[name='abc'][checked]").each(function () {
                    alert(this.value);
                });

jquery版本在1.3之後時,擷取checkbox的選中項的操作:

複製代碼 代碼如下:
                $("input[name='abc']:checked").each(function () {
                    alert(this.value);
                });
 

附 完整測試Demo代碼:


複製代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="js/jquery-1.7.2.min.js"></script>

    <script>
        $(function () {
            //擷取選中項(FF和chrome下無效)
            $('#huoqu').click(function () {

                //$("input[name='abc'][checked]").each(function () {
                //    alert(this.value);
                //});

                $('#show').html("");
                $("input[name='abc'][checked]").each(function () {
                    //alert(this.value);
                    $('#show').append(this.value + "  ");
                });
            });


            //擷取選中項
            $('#huoqu2').click(function () {
                $('#show').html("");
                $("input[name='abc']:checked").each(function () {
                    //alert(this.value);
                    $('#show').append(this.value + "  ");
                });
            });


            //全選/取消全選
            $('#quanxuan').toggle(function () {
                $("input[name='abc']").attr("checked", 'true');
            }, function () {
                $("input[name='abc']").removeAttr("checked");
            });


            //反選
            $('#fanxuan').click(function () {
                $("input[name='abc']").each(function () {
                    if ($(this).attr("checked")) {
                        $(this).removeAttr("checked");
                    } else {
                        $(this).attr("checked", 'true');
                    }
                });
            });
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="checkbox" name="abc" value="一年級" id="in1" checked="checked" /><label for="in1">一年級</label>
            <input type="checkbox" name="abc" value="二年級" id="in2" /><label for="in2">二年級</label>
            <input type="checkbox" name="abc" value="三年級" id="in3" /><label for="in3">三年級</label>
            <input type="checkbox" name="abc" value="四年級" id="in4" /><label for="in4">四年級</label>
            <input type="checkbox" name="abc" value="五年級" id="in5" /><label for="in5">五年級</label>
            <input type="checkbox" name="abc" value="六年級" id="in6" /><label for="in6">六年級</label>
            <input type="checkbox" name="abc" value="七年級" id="in7" /><label for="in7">七年級</label>
            <input type="checkbox" name="abc" value="八年級" id="in8" /><label for="in8">八年級</label>
        </div>
        <br />
        <input type="button" id="huoqu" value="擷取選中項(FF和chrome下無效)" />
        <input type="button" id="quanxuan" value="全選/取消全選" />
        <input type="button" id="fanxuan" value="反選" />
        <input type="button" id="huoqu2" value="擷取選中項" />
       <br />
        選中項: <div id="show">

        </div>
    </form>
</body>
</html>


作者:酷小孩

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.