温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

bootstrap+jQuery 实现下拉菜单中复选框全选和全不选效果

发布时间:2020-10-17 11:41:09 来源:脚本之家 阅读:275 作者:多小啦 栏目:web开发

前言

最近几天在公司做了个后台管理系统的小模块,其中有个功能是实现复选框的全选和全不选,用bootstrap和jQuery来实现。

效果是这样:

bootstrap+jQuery 实现下拉菜单中复选框全选和全不选效果

因为是内部用,样式也不要求太好看,直接上代码。

示例代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <META content="IE=11.0000" http-equiv="X-UA-Compatible">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" href="lib/bootstrap.min.css" rel="external nofollow" />
  <link rel="stylesheet" href="style.css" rel="external nofollow" />
  <style type="text/css">
  .dropdown-menu {
    width: 500px;
    height: 170px;
    padding: 0;
  }

  .dropdown-menu .all {
    width: 100%;
    height: 44px;
    background-color: #f9f9fa;
    line-height: 44px;
    padding-left: 10px;
  }

  .dropdown-menu input[type="checkbox"] {
    margin-left: 20px;
  }

  label.checkbox {
    display: inline-block;
  }

  .choose_bank label {
    margin-bottom: 16px;
  }
  </style>
</head>

<body>
  <div class="btn-group choose_bank" >
    <button class="btn btn-mini" >全部</button>
    <button class="btn btn-mini dropdown-toggle" data-toggle="dropdown" >
      <span class="caret"></span>
    </button>
    <div class="dropdown-menu">
      <label class="all">
        <input type="checkbox"> 全部
      </label>
      <label>
        <input type="checkbox"> 工商银行
      </label>
      <label>
        <input type="checkbox"> 农业银行
      </label>
      <label>
        <input type="checkbox"> 中国银行
      </label>
      <label>
        <input type="checkbox"> 建设银行
      </label>
      <label>
        <input type="checkbox"> 交通银行
      </label>
      <label>
        <input type="checkbox"> 邮政银行
      </label>
      <label>
        <input type="checkbox"> 招商银行
      </label>
      <label>
        <input type="checkbox"> 中信银行
      </label>
      <label>
        <input type="checkbox"> 民生银行
      </label>
      <label>
        <input type="checkbox"> 光大银行
      </label>
      <label>
        <input type="checkbox"> 平安银行
      </label>
      <label>
        <input type="checkbox"> 北京银行
      </label>
    </div>
  </div>
</body>
<script src="lib/jquery.min.js"></script>
<script src="lib/bootstrap.min.js"></script>
<script>
var banks = $('.all').siblings().children();
$('.all>input').click(function() {
  var flag = $(this).prop('checked');
  banks.prop('checked', flag);
})
banks.click(function() {
  // 如果有一个没选中,全选按钮不选中
  // 如果全部选中,全选按钮被选中
  var num = 0;
  banks.each(function() {
    if ($(this).prop("checked")) {
      num++;
    }
  })
  if (num == banks.length) {
    $('.all>input').prop('checked', true);
  } else {
    $('.all>input').prop('checked', false);
  }
})
</script>

</html>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI