加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bootstrap之表格checkbox复选框全选

发布时间:2020-12-17 20:55:06 所属栏目:安全 来源:网络整理
导读:效果图: HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了: [html] ? view plain ?copy table ? class = "table?table-bordered?table-hover" ?? ???? thead ?? ???????? tr ? class = "success" ???????????? th 类别编号

效果图:



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

[html]? view plain ?copy
  1. <table?class="table?table-bordered?table-hover">??
  2. ????thead>??
  3. ????????tr?class="success" ????????????th>类别编号</ ????????????>类别名称>类别组>状态>说明tr ????tbody ????????td>C00001>机车>有效>机车头>C00002>车厢>载客车厢table>??
重点是JS的实现。复选框很小,不容易点到,所以点击每一行也可以选中该行,并用高亮一些CSS样式表示。点击复选框所在单元格也能选中复选框。下面是完整代码和注释说明:

copy

    <!DOCTYPE?html html?lang="zh-CN" ??
  1. headmeta?charset="utf-8"meta?http-equiv="X-UA-Compatible"?content="IE=edge"meta?name="viewport"?content="width=device-width,?initial-scale=1" ????????<!--?上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后!?-->??
  2. title>表格meta?name="keywords"?content="表格"meta?name="description"?content="这真的是一个表格"?/>??
  3. meta?name="HandheldFriendly"?content="True"?/>??
  4. link?rel="shortcut?icon"?href="img/favicon.ico"<!--?Bootstrap3.3.5?CSS?-->??
  5. link?href="css/bootstrap.min.css"?rel="stylesheet" ??
  6. ????????<!--?HTML5?shim?and?Respond.js?for?IE8?support?of?HTML5?elements?and?media?queries?-->??
  7. ????????<!--[if?lt?IE?9]script?src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js">scriptscript?src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js" ????????<![endif]--bodydiv?class="panel-group"div?class="panel?panel-primary" ????????????????div?class="panel-heading" ????????????????????列表??
  8. 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>新增??
  9. button ???????????????????????? ????????????????????????????span?class="glyphicon?glyphicon-pencil"?aria-hidden="true">修改??
  10. span?class="glyphicon?glyphicon-remove"?aria-hidden="true">删除??
  11. ????????????????????div?class="panel-footer"navul?class="pagination?pagination-sm"li?class="disabled" ????????????????????????????????a?href="#"?aria-label="Previous" ????????????????????????????????????span?aria-hidden="true">?alili?class="active"a?href="#">1>2>3>4>5a?href="#"?aria-label="Next">?ul><!--?end?of?panel-footer?-->??
  12. <!--?end?of?panel?-->??
  13. <!--?jQuery1.11.3?(necessary?for?Bo?otstrap's?JavaScript?plugins)?-->??
  14. script?src="js/jquery-1.11.3.min.js?"<!--?Include?all?compiled?plugins?(below),?or?include?individual?files?as?needed?-->??
  15. script?src="js/bootstrap.min.js?" ????????$(function(){??
  16. ????????????function?initTableCheckbox()?{??
  17. ????????????????var?$thr?=?$('table?thead?tr');??
  18. ????????????????var?$checkAllTh?=?$('input?type="checkbox"?id="checkAll"?name="checkAll"?/>>');??
  19. ????????????????/*将全选/反选复选框添加到表头最前,即增加一列*/??
  20. ????????????????$thr.prepend($checkAllTh);??
  21. ????????????????/*“全选/反选”复选框*/??
  22. ????????????????var?$checkAll?=?$thr.find('input');??
  23. ????????????????$checkAll.click(function(event){??
  24. ????????????????????/*将所有行的选中状态设成全选框的选中状态*/??
  25. ????????????????????$tbr.find('input').prop('checked',$(this).prop('checked'));??
  26. ????????????????????/*并调整所有选中行的CSS样式*/??
  27. ????????????????????if?($(this).prop('checked'))?{??
  28. ????????????????????????$tbr.find('input').parent().parent().addClass('warning');??
  29. ????????????????????}?else{??
  30. ????????????????????????$tbr.find('input').parent().parent().removeClass('warning');??
  31. ????????????????????}??
  32. ????????????????????/*阻止向上冒泡,以防再次触发点击操作*/??
  33. ????????????????????event.stopPropagation();??
  34. ????????????????});??
  35. ????????????????/*点击全选框所在单元格时也触发全选框的点击操作*/??
  36. ????????????????$checkAllTh.click(function(){??
  37. ????????????????????$(this).find('input').click();??
  38. ????????????????var?$tbr?=?$('table?tbody?tr');??
  39. ????????????????var?$checkItemTd?=?$('input?type="checkbox"?name="checkItem"? ????????????????/*每一行都在最前面插入一个选中复选框的单元格*/??
  40. ????????????????$tbr.prepend($checkItemTd);??
  41. ????????????????/*点击每一行的选中复选框时*/??
  42. ????????????????$tbr.find('input').click(function(event){??
  43. ????????????????????/*调整选中行的CSS样式*/??
  44. ????????????????????$(this).parent().parent().toggleClass('warning');??
  45. ????????????????????/*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/??
  46. ????????????????????$checkAll.prop('checked',$tbr.find('input:checked').length?==?$tbr.length???true?:?false);??
  47. ????????????????????/*阻止向上冒泡,以防再次触发点击操作*/??
  48. ????????????????????event.stopPropagation();??
  49. ????????????????});??
  50. ????????????????/*点击每一行时也触发该行的选中操作*/??
  51. ????????????????$tbr.click(function(){??
  52. ????????????????????$(this).find('input').click();??
  53. ????????????}??
  54. ????????????initTableCheckbox();??
  55. ????????});??
  56. html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读