jquery實現checkbox 全選/全不選的通用寫法

來源:互聯網
上載者:User

jquery實現checkbox 全選/全不選的通用寫法

 本篇文章主要是對jquery實現checkbox 全選/全不選的通用寫法進行了介紹,需要的朋友可以過來參考下,希望對大家有所協助

 代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

    <script type="text/javascript">

        //name 是html表單專用於區別各組 chechbox 的屬性, 不要用id(必須唯一)或者其它屬性

        function getValues() {

            var aV = getCheckboxValues("a");

            var bV = getCheckboxValues("b");

            var cV = getCheckboxValues("c");

 

            var result = "a組選中值: " + (aV=="" ? "未選中任何值" : aV) + "n" +

                "b組選中值: " + (bV == "" ? "未選中任何值" : bV) + "n" +

                "c組選中值: " + (cV == "" ? "未選中任何值" : cV);

 

            alert(result);

        }

 

        function getCheckboxValues(name) {

            return $(":checkbox[name="+name+"]:checked").map(function(){  

                return $(this).val(); 

            }).get().join(",");

        }

 

        function chkAll(obj) {

            var name = $(obj).attr("name");

            //也許你覺得我寫得太複雜, 但jquery 版本眾多, 可以學點東西總是好的

            //1. jquery 1.6 以上

            //$(":checkbox[name=" + name.substring(0, 1) + "]").prop("checked", $(obj).prop("checked"));

            //2. jquery 1.6 以下

            //$(":checkbox[name=" + name.substring(0, 1) + "]").attr("checked", $(obj).attr("checked"));

            //3. 通用寫法

            $(":checkbox[name=" + name.substring(0, 1) + "]").each(function () {

                this.checked = obj.checked;

            });

        }

    </script>

</head>

<body>

    <div >

        <span>第1組(a):<input type="checkbox" name="aAll" onclick="chkAll(this)" />全選/全不選</span>

        <input type="checkbox" name="a" value="a1" checked="checked" />a1

        <input type="checkbox" name="a" value="a2" checked="checked"  />a2

        <input type="checkbox" name="a" value="a3" />a3

        <input type="checkbox" name="a" value="a4" />a4<br />

 

        <span>第2組(b):<input type="checkbox" name="bAll" onclick="chkAll(this)" />全選/全不選</span>

        <input type="checkbox" name="b" value="b1" />b1

        <input type="checkbox" name="b" value="b2" checked="checked" />b2

        <input type="checkbox" name="b" value="b3" checked="checked"  />b3

        <input type="checkbox" name="b" value="b4" />b4<br />

 

        <span>第3組(c):<input type="checkbox" name="cAll" onclick="chkAll(this)" />全選/全不選</span>

        <input type="checkbox" name="c" value="c1" />c1

        <input type="checkbox" name="c" value="c2" />c2

        <input type="checkbox" name="c" value="c3" checked="checked" />c3

        <input type="checkbox" name="c" value="c4" checked="checked"  />c4<br />

 

        <br />

        <input type="button" value="尋找每組的選中的值" onclick="getValues()" />

    </div>

</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.