<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>全選與反選</title> <!--此處引入的css與js需要更換成你本地的地址--> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" /> <script type="text/javascript" src="jquery-2.0.0/jquery.min.js"></script> <script> $(document).ready(function() { /*全選與反選按鈕*/ $("#all").click(function() { var state = $("#all").prop("checked"); if(state == true) { $("[name='option']").prop("checked", "checked"); } else { $("[name='option']").prop("checked", ""); } }); /*------------------------------------------------------------------------------------*/ /*根據復選框是否全部選中來判斷全選框是否選中*/ //獲取當前頁面數據條數 var options = $("[name='option']"); //給每一個復選框加上click事件 $("input[name='option']").click(function() { //獲取當前選中的復選框個數 var i = $("input[name='option']:checked"); //判斷復選框選中個數是否等於當前頁面數據條數 if(i.length == options.length) { //如果兩者值相等那么全選按鈕也會選中 $("#all").prop("checked", "checked"); } else { //當兩者值不相等時全選框便不會顯示選中 $("#all").prop("checked", ""); } }); }); </script> </head> <body> <div class="panel panel-default"> <div class="panel-heading">數據字典</div> <div class="panel-body"> <table class="table table-bordered table-hover"> <thead> <tr class="active"> <th style="width: 2%;"><input type="checkbox" id="all"></th> <th style="width: 28%;">字典ID</th> <th style="width: 15%;">字典key</th> <th style="width: 15%;">字典value</th> <th style="width: 15%;">創建時間</th> <th style="width: 25%;">編輯</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox" name="option"></td> <td>Bangalore</td> <td>560001</td> <td>Bangalore</td> <td>560001</td> <td> <button type="button" class="btn btn-info btn-xs">編輯</button> <button type="button" class="btn btn-danger btn-xs">刪除</button> </td> </tr> <tr> <td><input type="checkbox" name="option"></td> <td>Bangalore</td> <td>560001</td> <td>Bangalore</td> <td>560001</td> <td> <button type="button" class="btn btn-info btn-xs">編輯</button> <button type="button" class="btn btn-danger btn-xs">刪除</button> </td> </tr> <tr> <td><input type="checkbox" name="option"></td> <td>Bangalore</td> <td>560001</td> <td>Bangalore</td> <td>560001</td> <td> <button type="button" class="btn btn-info btn-xs">編輯</button> <button type="button" class="btn btn-danger btn-xs">刪除</button> </td> </tr> <tr> <td><input type="checkbox" name="option"></td> <td>Bangalore</td> <td>560001</td> <td>Bangalore</td> <td>560001</td> <td> <button type="button" class="btn btn-info btn-xs">編輯</button> <button type="button" class="btn btn-danger btn-xs">刪除</button> </td> </tr> </tbody> </table> <nav aria-label="Page navigation" class="pull-right"> <ul class="pagination"> <li> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li> <a href="#">1</a> </li> <li> <a href="#">2</a> </li> <li> <a href="#">3</a> </li> <li> <a href="#">4</a> </li> <li> <a href="#">5</a> </li> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div> </div> </body> </html>
