table表格的checkbox的全选和选中
2016-12-22 14:26
253 查看
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>表格</title> <meta name="keywords" content="表格"> <meta name="description" content="这真的是一个表格" /> <meta name="HandheldFriendly" content="True" /> <!-- Bootstrap3.3.5 CSS --> <link href="static/bootstrap/2.3.1/css_cerulean/bootstrap.min.css" type="text/css" rel="stylesheet"/> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <!--<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>--> <!--<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>--> <!--<![endif]–>--> </head> <body> <div class="panel-group"> <div class="panel panel-primary"> <div class="panel-heading"> 列表 </div> <div class="panel-body"> <div class="list-op" id="list_op"> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增 </button> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改 </button> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除 </button> </div> </div> <table class="table table-bordered table-hover"> <thead> <tr class="success"> <th>类别编号</th> <th>类别名称</th> <th>类别组</th> <th>状态</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>C00001</td> <td>机车</td> <td>机车</td> <td>有效</td> <td>机车头</td> </tr> <tr> <td>C00002</td> <td>车厢</td> <td>机车</td> <td>有效</td> <td>载客车厢</td> </tr> </tbody> </table> <div class="panel-footer"> <nav> <ul class="pagination pagination-sm"> <li class="disabled"> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li class="active"><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><!-- end of panel-footer --> </div><!-- end of panel --> </div> <!-- jQuery1.11.3 (necessary for Bo otstrap's JavaScript plugins) --> <script src="static/jquery/jquery-1.8.3.min.js" type="text/javascript"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="static/bootstrap/2.3.1/js/bootstrap.min.js" type="text/javascript"></script> <script> $(function(){ function initTableCheckbox() { var $thr = $('table thead tr'); var $tbr = $('table tbody tr'); var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>'); /*将全选/反选复选框添加到表头最前,即增加一列*/ $thr.prepend($checkAllTh); /*“全选/反选”复选框*/ var $checkAll = $thr.find('input'); $checkAll.click(function(event){ /*将所有行的选中状态设成全选框的选中状态*/ $tbr.find('input').prop('checked',$(this).prop('checked')); /*并调整所有选中行的CSS样式*/ if ($(this).prop('checked')) { $tbr.find('input').parent().parent().addClass('warning'); } else{ $tbr.find('input').parent().parent().removeClass('warning'); } /*阻止向上冒泡,以防再次触发点击操作*/ event.stopPropagation(); }); /*点击全选框所在单元格时也触发全选框的点击操作*/ $checkAllTh.click(function(){ $(this).find('input').click(); }); var $checkItemTd = $('<td><input type="checkbox" name="checkItem" /></td>'); /*每一行都在最前面插入一个选中复选框的单元格*/ $tbr.prepend($checkItemTd); /*点击每一行的选中复选框时*/ $tbr.find('input').click(function(event){ /*调整选中行的CSS样式*/ $(this).parent().parent().toggleClass('warning'); /*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/ $checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false); /*阻止向上冒泡,以防再次触发点击操作*/ event.stopPropagation(); }); /*点击每一行时也触发该行的选中操作*/ $tbr.click(function(){ $(this).find('input').click(); }); } initTableCheckbox(); }); </script> </body> </html>
相关文章推荐
- 实现前端table中checkbox全选功能,并将选中的数据发送到后端
- jQuery实现table表格checkbox全选的方法分析
- jQuery注册表格(table)行(tr)点击选中checkbox事件
- jquery实现checkbox全选,选择性勾选,以及table中点击tr中任意一行实现选中或不选中
- easy-ui中表格中动态添加checkbox,和全选事件和判断选中事件
- table 里面的 checkbox 全选,取消,单选,选中一行后选中行高亮
- HTML之Table简约时尚CSS样式 + HTML中checkbox元素【全选】或【取消全选】、或得到选中个数全案例使用详解
- jquery实现table表格选中后变颜色
- Jquery 遍历 Table;遍历CheckBox ;遍历Select;全选/全不选
- jquery checkbox 选中 全选 插件
- asp.net gridview中添加checkbox列,用于全部选中记录或取消全选
- JQuery 判断checkbox是否选中,checkbox全选,获取checkbox选中值
- asp.net TreeView的选中CheckBox触发事件(父对象选中子对象全选)
- JS选中checkbox后获取table内一行TD所有数据的方法
- checkbox 全部选中,和取消全选
- 如何记录ListView滚动后CheckBox保持选中状态以及当全选后让滚动出来的item也被选中
- bootstrapTable checkbox 选中 回显
- jQuery_review之table中根据行选中,进行背景变色和checkbox选中
- WPF 选中datagrid列头中checkbox则全选对应整列checkbox
- JQuery checkBox 全选 、获取选中的个数