RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1543666
Accepted
Andy_bat
Andy_bat
Asked:2023-10-02 21:59:05 +0000 UTC2023-10-02 21:59:05 +0000 UTC 2023-10-02 21:59:05 +0000 UTC

从图书馆自定义的复选框中获取值并以表单发送

  • 772

我决定用这种复选框来装饰我的表单

https://www.codehim.com/bootstrap/bootstrap-toggle-switch-with-text/ 一切正常,但是我现在如何获取元素的状态,设计是基于按钮的?形体空空飞走。

javascript
  • 1 1 个回答
  • 40 Views

1 个回答

  • Voted
  1. Best Answer
    Arbery
    2023-10-02T23:13:02Z2023-10-02T23:13:02Z

    以下是如何使用它,如文档中所写 - 在我看来,如果仅在 js 中,您可以获取类的存在/不存在active并将其添加到表单中。

    此自定义复选框本身,即使配置为label+checkbox,也不允许切换复选框并将其传输到表单。

    但是,如果您放弃bootstrap.min.js并编写自己的处理程序来添加活动类,则可以稍微简化任务。大约如下面的代码所示。要检查复选框的操作,只需暂时注释掉样式的最后一行display: none

    而且,是的,bootsrap 具有相同的带有复选框的开关,只需对样式进行一些修改,可能会适合您。

    document.querySelector('.btn').addEventListener('click', e => {
      e.target.classList.toggle('active');
    })
    .btn-toggle {
      margin: 0 4rem;
      padding: 0;
      position: relative;
      border: none;
      height: 1.5rem;
      width: 3rem;
      border-radius: 1.5rem;
      color: #6b7381;
      background: #bdc1c8;
    }
    
    .btn-toggle:focus,
    .btn-toggle.focus,
    .btn-toggle:focus.active,
    .btn-toggle.focus.active {
      outline: none;
    }
    
    .btn-toggle:before,
    .btn-toggle:after {
      line-height: 1.5rem;
      width: 4rem;
      text-align: center;
      font-weight: 600;
      font-size: 0.75rem;
      text-transform: uppercase;
      letter-spacing: 2px;
      position: absolute;
      bottom: 0;
      transition: opacity 0.25s;
    }
    
    .btn-toggle:before {
      content: 'Off';
      left: -4rem;
    }
    
    .btn-toggle:after {
      content: 'On';
      right: -4rem;
      opacity: 0.5;
    }
    
    .btn-toggle>.handle {
      position: absolute;
      top: 0.1875rem;
      left: 0.1875rem;
      width: 1.125rem;
      height: 1.125rem;
      border-radius: 1.125rem;
      background: #fff;
      transition: left 0.25s;
    }
    
    .btn-toggle.active {
      transition: background-color 0.25s;
    }
    
    .btn-toggle.active>.handle {
      left: 1.6875rem;
      transition: left 0.25s;
    }
    
    .btn-toggle.active:before {
      opacity: 0.5;
    }
    
    .btn-toggle.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-sm:before,
    .btn-toggle.btn-sm:after {
      line-height: -0.5rem;
      color: #fff;
      letter-spacing: 0.75px;
      left: 0.4125rem;
      width: 2.325rem;
    }
    
    .btn-toggle.btn-sm:before {
      text-align: right;
    }
    
    .btn-toggle.btn-sm:after {
      text-align: left;
      opacity: 0;
    }
    
    .btn-toggle.btn-sm.active:before {
      opacity: 0;
    }
    
    .btn-toggle.btn-sm.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-xs:before,
    .btn-toggle.btn-xs:after {
      display: none;
    }
    
    .btn-toggle:before,
    .btn-toggle:after {
      color: #6b7381;
    }
    
    .btn-toggle.active {
      background-color: #29b5a8;
    }
    
    .btn-toggle.btn-lg {
      margin: 0 5rem;
      padding: 0;
      position: relative;
      border: none;
      height: 2.5rem;
      width: 5rem;
      border-radius: 2.5rem;
    }
    
    .btn-toggle.btn-lg:focus,
    .btn-toggle.btn-lg.focus,
    .btn-toggle.btn-lg:focus.active,
    .btn-toggle.btn-lg.focus.active {
      outline: none;
    }
    
    .btn-toggle.btn-lg:before,
    .btn-toggle.btn-lg:after {
      line-height: 2.5rem;
      width: 5rem;
      text-align: center;
      font-weight: 600;
      font-size: 1rem;
      text-transform: uppercase;
      letter-spacing: 2px;
      position: absolute;
      bottom: 0;
      transition: opacity 0.25s;
    }
    
    .btn-toggle.btn-lg:before {
      content: 'Off';
      left: -5rem;
    }
    
    .btn-toggle.btn-lg:after {
      content: 'On';
      right: -5rem;
      opacity: 0.5;
    }
    
    .btn-toggle.btn-lg>.handle {
      position: absolute;
      top: 0.3125rem;
      left: 0.3125rem;
      width: 1.875rem;
      height: 1.875rem;
      border-radius: 1.875rem;
      background: #fff;
      transition: left 0.25s;
    }
    
    .btn-toggle.btn-lg.active {
      transition: background-color 0.25s;
    }
    
    .btn-toggle.btn-lg.active>.handle {
      left: 2.8125rem;
      transition: left 0.25s;
    }
    
    .btn-toggle.btn-lg.active:before {
      opacity: 0.5;
    }
    
    .btn-toggle.btn-lg.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-lg.btn-sm:before,
    .btn-toggle.btn-lg.btn-sm:after {
      line-height: 0.5rem;
      color: #fff;
      letter-spacing: 0.75px;
      left: 0.6875rem;
      width: 3.875rem;
    }
    
    .btn-toggle.btn-lg.btn-sm:before {
      text-align: right;
    }
    
    .btn-toggle.btn-lg.btn-sm:after {
      text-align: left;
      opacity: 0;
    }
    
    .btn-toggle.btn-lg.btn-sm.active:before {
      opacity: 0;
    }
    
    .btn-toggle.btn-lg.btn-sm.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-lg.btn-xs:before,
    .btn-toggle.btn-lg.btn-xs:after {
      display: none;
    }
    
    .btn-toggle.btn-sm {
      margin: 0 0.5rem;
      padding: 0;
      position: relative;
      border: none;
      height: 1.5rem;
      width: 3rem;
      border-radius: 1.5rem;
    }
    
    .btn-toggle.btn-sm:focus,
    .btn-toggle.btn-sm.focus,
    .btn-toggle.btn-sm:focus.active,
    .btn-toggle.btn-sm.focus.active {
      outline: none;
    }
    
    .btn-toggle.btn-sm:before,
    .btn-toggle.btn-sm:after {
      line-height: 1.5rem;
      width: 0.5rem;
      text-align: center;
      font-weight: 600;
      font-size: 0.55rem;
      text-transform: uppercase;
      letter-spacing: 2px;
      position: absolute;
      bottom: 0;
      transition: opacity 0.25s;
    }
    
    .btn-toggle.btn-sm:before {
      content: 'Off';
      left: -0.5rem;
    }
    
    .btn-toggle.btn-sm:after {
      content: 'On';
      right: -0.5rem;
      opacity: 0.5;
    }
    
    .btn-toggle.btn-sm>.handle {
      position: absolute;
      top: 0.1875rem;
      left: 0.1875rem;
      width: 1.125rem;
      height: 1.125rem;
      border-radius: 1.125rem;
      background: #fff;
      transition: left 0.25s;
    }
    
    .btn-toggle.btn-sm.active {
      transition: background-color 0.25s;
    }
    
    .btn-toggle.btn-sm.active>.handle {
      left: 1.6875rem;
      transition: left 0.25s;
    }
    
    .btn-toggle.btn-sm.active:before {
      opacity: 0.5;
    }
    
    .btn-toggle.btn-sm.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-sm.btn-sm:before,
    .btn-toggle.btn-sm.btn-sm:after {
      line-height: -0.5rem;
      color: #fff;
      letter-spacing: 0.75px;
      left: 0.4125rem;
      width: 2.325rem;
    }
    
    .btn-toggle.btn-sm.btn-sm:before {
      text-align: right;
    }
    
    .btn-toggle.btn-sm.btn-sm:after {
      text-align: left;
      opacity: 0;
    }
    
    .btn-toggle.btn-sm.btn-sm.active:before {
      opacity: 0;
    }
    
    .btn-toggle.btn-sm.btn-sm.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-sm.btn-xs:before,
    .btn-toggle.btn-sm.btn-xs:after {
      display: none;
    }
    
    .btn-toggle.btn-xs {
      margin: 0 0;
      padding: 0;
      position: relative;
      border: none;
      height: 1rem;
      width: 2rem;
      border-radius: 1rem;
    }
    
    .btn-toggle.btn-xs:focus,
    .btn-toggle.btn-xs.focus,
    .btn-toggle.btn-xs:focus.active,
    .btn-toggle.btn-xs.focus.active {
      outline: none;
    }
    
    .btn-toggle.btn-xs:before,
    .btn-toggle.btn-xs:after {
      line-height: 1rem;
      width: 0;
      text-align: center;
      font-weight: 600;
      font-size: 0.75rem;
      text-transform: uppercase;
      letter-spacing: 2px;
      position: absolute;
      bottom: 0;
      transition: opacity 0.25s;
    }
    
    .btn-toggle.btn-xs:before {
      content: 'Off';
      left: 0;
    }
    
    .btn-toggle.btn-xs:after {
      content: 'On';
      right: 0;
      opacity: 0.5;
    }
    
    .btn-toggle.btn-xs>.handle {
      position: absolute;
      top: 0.125rem;
      left: 0.125rem;
      width: 0.75rem;
      height: 0.75rem;
      border-radius: 0.75rem;
      background: #fff;
      transition: left 0.25s;
    }
    
    .btn-toggle.btn-xs.active {
      transition: background-color 0.25s;
    }
    
    .btn-toggle.btn-xs.active>.handle {
      left: 1.125rem;
      transition: left 0.25s;
    }
    
    .btn-toggle.btn-xs.active:before {
      opacity: 0.5;
    }
    
    .btn-toggle.btn-xs.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-xs.btn-sm:before,
    .btn-toggle.btn-xs.btn-sm:after {
      line-height: -1rem;
      color: #fff;
      letter-spacing: 0.75px;
      left: 0.275rem;
      width: 1.55rem;
    }
    
    .btn-toggle.btn-xs.btn-sm:before {
      text-align: right;
    }
    
    .btn-toggle.btn-xs.btn-sm:after {
      text-align: left;
      opacity: 0;
    }
    
    .btn-toggle.btn-xs.btn-sm.active:before {
      opacity: 0;
    }
    
    .btn-toggle.btn-xs.btn-sm.active:after {
      opacity: 1;
    }
    
    .btn-toggle.btn-xs.btn-xs:before,
    .btn-toggle.btn-xs.btn-xs:after {
      display: none;
    }
    
    .btn-toggle.btn-secondary {
      color: #6b7381;
      background: #bdc1c8;
    }
    
    .btn-toggle.btn-secondary:before,
    .btn-toggle.btn-secondary:after {
      color: #6b7381;
    }
    
    .btn-toggle.btn-secondary.active {
      background-color: #ff8300;
    }
    
    .btn span {
      z-index: 10;
      opacity: 0;
      position: absolute;
      width: 100%;
      height: 100%;
      user-select: none;
    }
    
    input[type=checkbox] {
      display: none
    }
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css'>
    
    <label class="btn btn-lg btn-toggle" data-toggle="button" aria-pressed="true" autocomplete="off">
    <div class="handle"><input type="checkbox" value="1" ></div>
    
    </label>

    • 1

相关问题

  • 第二个 Instagram 按钮的 CSS 属性

  • 由于模糊,内容不可见

  • 弹出队列。消息显示不正确

  • 是否可以在 for 循环中插入提示?

  • 如何将 JSON 请求中的信息输出到数据表 Vuetify vue.js?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5