转自: http://blog.csdn.net/shangmingchao/article/details/49761315

效果图:

HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了:

  1. <table class="table table-bordered table-hover">
  2. <thead>
  3. <tr class="success">
  4. <th>类别编号</th>
  5. <th>类别名称</th>
  6. <th>类别组</th>
  7. <th>状态</th>
  8. <th>说明</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr>
  13. <td>C00001</td>
  14. <td>机车</td>
  15. <td>机车</td>
  16. <td>有效</td>
  17. <td>机车头</td>
  18. </tr>
  19. <tr>
  20. <td>C00002</td>
  21. <td>车厢</td>
  22. <td>机车</td>
  23. <td>有效</td>
  24. <td>载客车厢</td>
  25. </tr>
  26. </tbody>
  27. </table>

重点是JS的实现。复选框很小,不容易点到,所以点击每一行也可以选中该行,并用高亮一些CSS样式表示。点击复选框所在单元格也能选中复选框。下面是完整代码和注释说明:

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1">
    7. <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    8. <title>表格</title>
    9. <meta name="keywords" content="表格">
    10. <meta name="description" content="这真的是一个表格" />
    11. <meta name="HandheldFriendly" content="True" />
    12. <link rel="shortcut icon" href="img/favicon.ico">
    13. <!-- Bootstrap3.3.5 CSS -->
    14. <link href="css/bootstrap.min.css" rel="stylesheet">
    15. <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    16. <!--[if lt IE 9]>
    17. <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    18. <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    19. <![endif]-->
    20. </head>
    21. <body>
    22. <div class="panel-group">
    23. <div class="panel panel-primary">
    24. <div class="panel-heading">
    25. 列表
    26. </div>
    27. <div class="panel-body">
    28. <div class="list-op" id="list_op">
    29. <button type="button" class="btn btn-default btn-sm">
    30. <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
    31. </button>
    32. <button type="button" class="btn btn-default btn-sm">
    33. <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
    34. </button>
    35. <button type="button" class="btn btn-default btn-sm">
    36. <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
    37. </button>
    38. </div>
    39. </div>
    40. <table class="table table-bordered table-hover">
    41. <thead>
    42. <tr class="success">
    43. <th>类别编号</th>
    44. <th>类别名称</th>
    45. <th>类别组</th>
    46. <th>状态</th>
    47. <th>说明</th>
    48. </tr>
    49. </thead>
    50. <tbody>
    51. <tr>
    52. <td>C00001</td>
    53. <td>机车</td>
    54. <td>机车</td>
    55. <td>有效</td>
    56. <td>机车头</td>
    57. </tr>
    58. <tr>
    59. <td>C00002</td>
    60. <td>车厢</td>
    61. <td>机车</td>
    62. <td>有效</td>
    63. <td>载客车厢</td>
    64. </tr>
    65. </tbody>
    66. </table>
    67. <div class="panel-footer">
    68. <nav>
    69. <ul class="pagination pagination-sm">
    70. <li class="disabled">
    71. <a href="#" aria-label="Previous">
    72. <span aria-hidden="true">«</span>
    73. </a>
    74. </li>
    75. <li class="active"><a href="#">1</a></li>
    76. <li><a href="#">2</a></li>
    77. <li><a href="#">3</a></li>
    78. <li><a href="#">4</a></li>
    79. <li><a href="#">5</a></li>
    80. <li>
    81. <a href="#" aria-label="Next">
    82. <span aria-hidden="true">»</span>
    83. </a>
    84. </li>
    85. </ul>
    86. </nav>
    87. </div><!-- end of panel-footer -->
    88. </div><!-- end of panel -->
    89. </div>
    90. <!-- jQuery1.11.3 (necessary for Bo otstrap's JavaScript plugins) -->
    91. <script src="js/jquery-1.11.3.min.js "></script>
    92. <!-- Include all compiled plugins (below), or include individual files as needed -->
    93. <script src="js/bootstrap.min.js "></script>
    94. <script>
    95. $(function(){
    96. function initTableCheckbox() {
    97. var $thr = $('table thead tr');
    98. var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>');
    99. /*将全选/反选复选框添加到表头最前,即增加一列*/
    100. $thr.prepend($checkAllTh);
    101. /*“全选/反选”复选框*/
    102. var $checkAll = $thr.find('input');
    103. $checkAll.click(function(event){
    104. /*将所有行的选中状态设成全选框的选中状态*/
    105. $tbr.find('input').prop('checked',$(this).prop('checked'));
    106. /*并调整所有选中行的CSS样式*/
    107. if ($(this).prop('checked')) {
    108. $tbr.find('input').parent().parent().addClass('warning');
    109. } else{
    110. $tbr.find('input').parent().parent().removeClass('warning');
    111. }
    112. /*阻止向上冒泡,以防再次触发点击操作*/
    113. event.stopPropagation();
    114. });
    115. /*点击全选框所在单元格时也触发全选框的点击操作*/
    116. $checkAllTh.click(function(){
    117. $(this).find('input').click();
    118. });
    119. var $tbr = $('table tbody tr');
    120. var $checkItemTd = $('<td><input type="checkbox" name="checkItem" /></td>');
    121. /*每一行都在最前面插入一个选中复选框的单元格*/
    122. $tbr.prepend($checkItemTd);
    123. /*点击每一行的选中复选框时*/
    124. $tbr.find('input').click(function(event){
    125. /*调整选中行的CSS样式*/
    126. $(this).parent().parent().toggleClass('warning');
    127. /*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/
    128. $checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false);
    129. /*阻止向上冒泡,以防再次触发点击操作*/
    130. event.stopPropagation();
    131. });
    132. /*点击每一行时也触发该行的选中操作*/
    133. $tbr.click(function(){
    134. $(this).find('input').click();
    135. });
    136. }
    137. initTableCheckbox();
    138. });
    139. </script>
    140. </body>
    141. </html>

Bootstrap之表格checkbox复选框全选 [转]的更多相关文章

  1. Jquery表格变色 复选框全选,反选

    /*jquery静态表格变色*/ $(".tr2").mouseover(function(){ $(this).css("background"," ...

  2. checkbox复选框全选批量删除

    多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="Form ...

  3. jQuery 前端复选框 全选 反选 下拉菜单联动

    jQuery 页面中复选框全选.反选.下拉联动(级联) <!DOCTYPE html> <html lang="en"> <head> < ...

  4. jQuery中的几个案例:隔行变色、复选框全选和全不选

    1 表格隔行变色 1 技术分析: 1 )基本过滤选择器: odd: even: 2 )jq添加和移除样式: addClass(); removeClass(); 2 代码实现 <script s ...

  5. js 复选框 全选都选 如果某一个子复选框没选中 则全选按钮不选中

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>js 复选框 全选都选 ...

  6. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  7. 复选框全选、全不选和反选的效果实现VIEW:1592

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. html+css+js实现复选框全选与反选

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. jQuery 复选框全选/取消全选/反选

    jQuery实现的复选框全选/取消全选/反选及获得选择的值. 完整代码: <!DOCTYPE html> <html> <head> <script type ...

  10. js 判断 复选框全选、全不选、反选、必选一个

    一个挺 使用的 js 代码片段,  判断  复选框全选.全不选.反选.必选一个 记录下, 搬来的 思路: 修改数据的 选中与否状态, 拿到所有的输入框,看是否有选中的状态 <html> & ...

随机推荐

  1. 出力csv

    public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path) { if (!Sy ...

  2. apache 工作模式

    apache三种工作模式: prefork(2.4前默认)/worker/event(2.4默认)内容整理来自以下网站http://m.blog.csdn.net/article/details?id ...

  3. 如何更改Chrome默认的搜索引擎

    1 打开Chrome浏览器之后,点击窗口右上角的图标,在弹出的菜单中点击设置,如图所示: 2  在打开的窗口中,点击管理搜索引擎,如下图所示: 3 在弹出的窗口中,找到百度的搜索引擎或者bing的搜索 ...

  4. PHP学习心得(八)——运算符

    运算符是可以通过给出的一或多个值(用编程行话来说,表达式)来产生另一个值(因而整个结构成为一个表达式)的东西.所以可以认为函数或任何会返回一个值(例如 print)的结构是运算符,而那些没有返回值的( ...

  5. 【dynamic】简化反射简单尝试

    最近在自己瞎整设计自己的数据访问层(纯属深入了解C#用),遇到了反射.网传反射性能很差,可是我们项目中也有功能用到了反射,总体来说还不错(小项目).由于居安思危的感觉越发沉重,不得不去打破传统,去寻求 ...

  6. 汇编函数调用中bp和sp是指什么?

    bp为基址寄存器,一般在函数中用来保存进入函数时的sp的栈顶基址sp是栈顶指针,它每次指向栈顶.每次子函数调用时,系统在开始时都会保存这个两个指针并在函数结束时恢复sp和bp的值.像下面这样:在函数进 ...

  7. 李洪强漫谈iOS开发[C语言-017]-printf函数

  8. 李洪强漫谈iOS开发[C语言-021]-运算符

  9. easyui源码翻译1.32--SearchBox(搜索框)

    前言 使用$.fn.searchbox.defaults重写默认值对象.下载该插件翻译源码 搜索框提示用户需要输入搜索的值.它可以结合一个菜单,允许用户选择不同的搜索类别.在用户按下回车键或点击组件右 ...

  10. 【Xamarin挖墙脚系列:Xamarin.IOS机制原理剖析】

    原文:[Xamarin挖墙脚系列:Xamarin.IOS机制原理剖析] [注意:]团队里总是有人反映卸载Xamarin,清理不完全.之前写过如何完全卸载清理剩余的文件.今天写了Windows下的批命令 ...