jquery 書寫全選反選功能

來源:互聯網
上載者:User

標籤:

書寫一個後台管理中用到的全選反選功能。代碼如下

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>全選反選外掛程式</title>    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script></head><body><div class="container">    <div class="row6">        <table class="table table-bordered">            <thead>                <tr>                    <th style="width:250px;">                        <button type="button" class="btn btn-success" id="check_all">全選</button>                        <button type="button" class="btn btn-danger" id="check_others">反選</button>                    </th>                    <th>ID</th>                    <th>name</th>                </tr>            </thead>            <tbody>                <tr>                    <td><input type="checkbox" class="son_checkbox" ></td>                    <td>1</td>                    <td>張三</td>                </tr>                <tr>                    <td><input type="checkbox" class="son_checkbox" ></td>                    <td>2</td>                    <td>李四</td>                </tr>                <tr>                    <td><input type="checkbox" class="son_checkbox" ></td>                    <td>3</td>                    <td>王五</td>                </tr>                <tr>                    <td><input type="checkbox" class="son_checkbox" ></td>                    <td>4</td>                    <td>趙六</td>                </tr>            </tbody>        </table>    </div></div><script>    $(function(){        //點擊全選事件        $("#check_all").click(function(){            var text=$(this).text();            var status;            if(text=="全選")            {                status=true;                text="取消全選";            }            else if(text=="取消全選")            {                status=false;                text="全選";            }else            {                return false;            }            $(".son_checkbox").prop("checked",status);//改變狀態            $(this).text(text);//設定文字        });        //點擊反選事件        $("#check_others").click(function(){            //遍曆所有選擇框 然後反轉狀態            $(".son_checkbox").each(function(){                var now_status=$(this).prop("checked");//擷取當前選擇框是否選中                now_status=!now_status;//反轉狀態                $(this).prop("checked",now_status);//重新繫結狀態            });        });    });</script></body></html>

 如下:

jquery 書寫全選反選功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.