[開發筆記]-jQuery獲取checkbox選中項等操做及注意事項

      今天在作一個項目功能時須要顯示checkbox選項來讓用戶進行選擇,因爲前端不是很熟練,因此作了一個簡單的Demo,其中遇到一些小問題,特記錄下來,但願能幫到遇到相似問題的同窗們。html

1. 獲取checkbox的選中項前端

2. checkbox選項的全選 反選操做jquery

用於測試的checkbox代碼段:chrome

 <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瀏覽器下就沒法獲取當前的選中項,測試效果以下:app

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

IE10下的效果:ui

chrome瀏覽器下的效果:this

經過在google上搜索,找到了緣由: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>
相關文章
相關標籤/搜索