標籤:
書寫一個後台管理中用到的全選反選功能。代碼如下
<!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 書寫全選反選功能