1. 程式人生 > >jq表格全反選實例

jq表格全反選實例

remove order table 反選 body 單選 checked lec 表格

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .backcolor{ background: #8A2BE2; } </style> <script src="js/jquery-1.4.1.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript">
$(function(){ //單選 $("#tab tbody tr input").click(function(){ $(this).parent().parent().toggleClass("backcolor"); }); //全選、全不選 $("#selAll").click(function(){ $("#selAgainst").attr("checked",""); if($(this).attr("checked")){ $("#tab tbody tr").addClass("backcolor");
$("#tab tbody tr input[type=checkbox]").attr("checked","checked") }else{ $("#tab tbody tr").removeClass("backcolor"); $("#tab tbody tr input[type=checkbox]").removeAttr("checked") } }); //反選 $("#selAgainst").click(function(){ $("#selAll").attr("checked",""); $("#tab tbody tr").toggleClass("backcolor");
$("#tab tbody tr input[type=checkbox]").each(function(){ if($(this).attr("checked")!=true){ $(this).attr("checked","checked"); }else{ $(this).removeAttr("checked") } }); }); }); </script> </head> <body> <table border="1" width="350px" id="tab"> <thead> <tr> <th>學號</th> <th>姓名</th> <th>性別</th> <th>班級</th> </tr> <tr> <td colspan="4"><input type="checkbox" id="selAll"/>全選/全不選 <input type="checkbox" id="selAgainst"/>反選 </td> </tr> </thead> <tbody> <tr> <td><input type="checkbox"/>s001</td> <td>小白</td> <td>女</td> <td>t111</td> </tr> <tr> <td><input type="checkbox"/>s002</td> <td>小黑</td> <td>男</td> <td>t111</td> </tr> <tr> <td><input type="checkbox"/>s003</td> <td>小強</td> <td>男</td> <td>t112</td> </tr> <tr> <td><input type="checkbox"/>s004</td> <td>張三</td> <td>男</td> <td>t113</td> </tr> <tr> <td><input type="checkbox"/>s005</td> <td>小蘭</td> <td>女</td> <td>t113</td> </tr> </tbody> </table> </body></html>

jq表格全反選實例