Bootstrap之表格checkbox复选框全选 [转]
转自: http://blog.csdn.net/shangmingchao/article/details/49761315
效果图:
HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了:
- <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>
重点是JS的实现。复选框很小,不容易点到,所以点击每一行也可以选中该行,并用高亮一些CSS样式表示。点击复选框所在单元格也能选中复选框。下面是完整代码和注释说明:
- <!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" />
- <link rel="shortcut icon" href="img/favicon.ico">
- <!-- Bootstrap3.3.5 CSS -->
- <link href="css/bootstrap.min.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="js/jquery-1.11.3.min.js "></script>
- <!-- Include all compiled plugins (below), or include individual files as needed -->
- <script src="js/bootstrap.min.js "></script>
- <script>
- $(function(){
- function initTableCheckbox() {
- var $thr = $('table thead 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 $tbr = $('table tbody tr');
- 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>
Bootstrap之表格checkbox复选框全选 [转]的更多相关文章
- Jquery表格变色 复选框全选,反选
/*jquery静态表格变色*/ $(".tr2").mouseover(function(){ $(this).css("background"," ...
- checkbox复选框全选批量删除
多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="Form ...
- jQuery 前端复选框 全选 反选 下拉菜单联动
jQuery 页面中复选框全选.反选.下拉联动(级联) <!DOCTYPE html> <html lang="en"> <head> < ...
- jQuery中的几个案例:隔行变色、复选框全选和全不选
1 表格隔行变色 1 技术分析: 1 )基本过滤选择器: odd: even: 2 )jq添加和移除样式: addClass(); removeClass(); 2 代码实现 <script s ...
- js 复选框 全选都选 如果某一个子复选框没选中 则全选按钮不选中
<!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>js 复选框 全选都选 ...
- JavaScript小例子:复选框全选
JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...
- 复选框全选、全不选和反选的效果实现VIEW:1592
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- html+css+js实现复选框全选与反选
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- jQuery 复选框全选/取消全选/反选
jQuery实现的复选框全选/取消全选/反选及获得选择的值. 完整代码: <!DOCTYPE html> <html> <head> <script type ...
- js 判断 复选框全选、全不选、反选、必选一个
一个挺 使用的 js 代码片段, 判断 复选框全选.全不选.反选.必选一个 记录下, 搬来的 思路: 修改数据的 选中与否状态, 拿到所有的输入框,看是否有选中的状态 <html> & ...
随机推荐
- 出力csv
public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path) { if (!Sy ...
- apache 工作模式
apache三种工作模式: prefork(2.4前默认)/worker/event(2.4默认)内容整理来自以下网站http://m.blog.csdn.net/article/details?id ...
- 如何更改Chrome默认的搜索引擎
1 打开Chrome浏览器之后,点击窗口右上角的图标,在弹出的菜单中点击设置,如图所示: 2 在打开的窗口中,点击管理搜索引擎,如下图所示: 3 在弹出的窗口中,找到百度的搜索引擎或者bing的搜索 ...
- PHP学习心得(八)——运算符
运算符是可以通过给出的一或多个值(用编程行话来说,表达式)来产生另一个值(因而整个结构成为一个表达式)的东西.所以可以认为函数或任何会返回一个值(例如 print)的结构是运算符,而那些没有返回值的( ...
- 【dynamic】简化反射简单尝试
最近在自己瞎整设计自己的数据访问层(纯属深入了解C#用),遇到了反射.网传反射性能很差,可是我们项目中也有功能用到了反射,总体来说还不错(小项目).由于居安思危的感觉越发沉重,不得不去打破传统,去寻求 ...
- 汇编函数调用中bp和sp是指什么?
bp为基址寄存器,一般在函数中用来保存进入函数时的sp的栈顶基址sp是栈顶指针,它每次指向栈顶.每次子函数调用时,系统在开始时都会保存这个两个指针并在函数结束时恢复sp和bp的值.像下面这样:在函数进 ...
- 李洪强漫谈iOS开发[C语言-017]-printf函数
- 李洪强漫谈iOS开发[C语言-021]-运算符
- easyui源码翻译1.32--SearchBox(搜索框)
前言 使用$.fn.searchbox.defaults重写默认值对象.下载该插件翻译源码 搜索框提示用户需要输入搜索的值.它可以结合一个菜单,允许用户选择不同的搜索类别.在用户按下回车键或点击组件右 ...
- 【Xamarin挖墙脚系列:Xamarin.IOS机制原理剖析】
原文:[Xamarin挖墙脚系列:Xamarin.IOS机制原理剖析] [注意:]团队里总是有人反映卸载Xamarin,清理不完全.之前写过如何完全卸载清理剩余的文件.今天写了Windows下的批命令 ...