【问题标题】:Toggling the checked status of a html/css checkbox using js使用 js 切换 html/css 复选框的选中状态
【发布时间】:2016-08-12 03:27:00
【问题描述】:

我下载了一个带有预先构建的开关组件的主题,该组件取代了普通的复选框功能。这个开关非常适合 UI,我正在拼命尝试让它工作,但我无法让底层复选框的“已检查”状态在点击/触摸事件时更改。

这是html结构:

<div class="switch has-switch" data-off-label="<i class='fa fa-times'></i>" data-on-label="<i class='fa fa-check'></i>">
  <div class="switch-off switch-animate">
    <input checked type="checkbox">                                                           
    <span class="switch-left"><i class="fa fa-check"></i></span>
    <label>&nbsp;</label>
    <span class="switch-right"><i class="fa fa-times"></i></span>
  </div>
</div>

开关功能工作得很好,但我无法让它切换复选框输入属性的“已选中”值。

我尝试了一些解决方案。我试过的最后一个是这个(注意这是一个测试,看看我是否至少可以让它在点击时取消选中):

$(function () {
  $('.switch').click(function() {
    console.log(this);
    var CheckboxInput = $(this).find('input[type="checkbox"]');
    if ($(CheckboxInput).is(':checked')) {
      CheckboxInput.prop('checked', false);
    }
    }
  });
});

我的 javascript 知识不是很好(更像是一个 Rails 人)。谁能帮我找出我做错了什么?

【问题讨论】:

    标签: javascript jquery html checkbox


    【解决方案1】:

    索尔:

    $(function() {
      $('.switch').click(function() {
        var checkBoxes = $(this).find('input');
        checkBoxes.prop("checked", !checkBoxes.prop("checked"));
      });
    });
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
    
    <div class="switch has-switch" data-off-label="<i class='fa fa-times'></i>" data-on-label="<i class='fa fa-check'></i>">
      <div class="switch-off switch-animate">
        <input checked type="checkbox">
        <span class="switch-left"><i class="fa fa-check">LEFT</i></span>
        <label>&nbsp;</label>
        <span class="switch-right"><i class="fa fa-times"></i>RIGHT</span>
      </div>
    </div>

    一个没有 JAVASCRIPT 的简单 SOLN:

    .switch {
      position: relative;
      display: block;
      vertical-align: top;
      width: 100px;
      height: 30px;
      padding: 3px;
      margin: 0 10px 10px 0;
      background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
      background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
      border-radius: 18px;
      box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
      cursor: pointer;
    }
    .switch-input {
      position: absolute;
      top: 0;
      left: 200px;
      opacity: 50;
    }
    .switch-label {
      position: relative;
      display: block;
      height: inherit;
      font-size: 10px;
      text-transform: uppercase;
      background: #eceeef;
      border-radius: inherit;
      box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
    }
    .switch-label:before,
    .switch-label:after {
      position: absolute;
      top: 50%;
      margin-top: -.5em;
      line-height: 1;
      -webkit-transition: inherit;
      -moz-transition: inherit;
      -o-transition: inherit;
      transition: inherit;
    }
    .switch-label:before {
      content: attr(data-off);
      right: 11px;
      color: #aaaaaa;
      text-shadow: 0 1px rgba(255, 255, 255, 0.5);
    }
    .switch-label:after {
      content: attr(data-on);
      left: 11px;
      color: #FFFFFF;
      text-shadow: 0 1px rgba(0, 0, 0, 0.2);
      opacity: 0;
    }
    .switch-input:checked ~ .switch-label {
      background: #E1B42B;
      box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
    }
    .switch-input:checked ~ .switch-label:before {
      opacity: 0;
    }
    .switch-input:checked ~ .switch-label:after {
      opacity: 1;
    }
    .switch-handle {
      position: absolute;
      top: 4px;
      left: 4px;
      width: 28px;
      height: 28px;
      background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
      background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
      border-radius: 100%;
      box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    }
    .switch-handle:before {
      content: "";
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -6px 0 0 -6px;
      width: 12px;
      height: 12px;
      background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
      background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
      border-radius: 6px;
      box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
    }
    .switch-input:checked ~ .switch-handle {
      left: 74px;
      box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
    }
    .switch-label,
    .switch-handle {
      transition: All 0.3s ease;
      -webkit-transition: All 0.3s ease;
      -moz-transition: All 0.3s ease;
      -o-transition: All 0.3s ease;
    }
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
    <label class="switch">
      <input class="switch-input" type="checkbox" />
      <span class="switch-label" data-on="ONL" data-off="Off"></span> 
      <span class="switch-handle"></span> 
    </label>

    【讨论】:

    • 感谢@iceman。这使用实体的“名称”属性吗?我正在寻找在组件级别添加此功能的东西。这样,只要我使用这个 css/html 组件,它就会起作用。有没有一种方法可以让您的解决方案与输入类型一起使用?更具体地说,问题中给出的 HTML 结构中的复选框输入。
    • 确定要绑定的属性或条件到底是什么?我会相应地编辑。
    • @Herm cud u 创建一个 fiddle 或 sn-p 工作示例,我可以将 soln 集成到其中,对我们双方来说都会更容易。
    • 我希望它具有切换功能,任何复选框输入都放置在我的原始问题中指示的 html 结构内。因此使用 .switch 类并查找类型为“复选框”的子元素并切换所述元素的选中状态。
    • 不,它没有。哇,我对您的乐于助人感到惊讶。如果您右键单击 sn-p 并检查元素,您会发现它不会切换输入的属性值。我真的很感谢你的彻底帮助!!!!!!
    【解决方案2】:

    替换这一行

    if ($(CheckboxInput).is(':checked')) {
      CheckboxInput.prop('checked', false);
    }
    

    CheckboxInput.prop('checked', !$(CheckboxInput).is(':checked'));
    

    工作小提琴:https://jsfiddle.net/codeandcloud/xnq16htu/

    更新:已将其作为错误提交,团队已将其视为无效错误报告和注释,

    这是正确的行为。用户的点击不会删除 来自 HTML 的属性。它只会改变属性。如果你想 知道动态,使用 .prop("checked") 代替。

    链接:https://bugs.jquery.com/ticket/10730

    测试:使用 Vanilla JavaScript

    var container = document.getElementById("container"),
        elm = document.getElementById("test-check");
    elm.addEventListener("click", function() {
        console.log("CheckBox Markup: ", container.innerHTML);
        console.log("CheckBox Status: ", elm.checked);
    });
    <div id="container">
        <input id="test-check" type="checkbox" checked />
    </div>

    【讨论】:

    • 感谢@naveen 的快速回复。它仍然没有切换输入的检查状态
    • 这似乎没有切换 html 中的“checked”属性。如果您在小提琴上使用 firebug 或 chrome dev 控制台,您将能够明白我的意思。它确实检查和取消检查ui上的输入,但代码中的属性没有添加/删除
    【解决方案3】:

    您只需将属性 'true' 替换为 'checked' 即 CheckboxInput.prop('checked','checked');

            <div class="switch has-switch" data-off-label="<i class='fa fa-times'></i>" data-on-label="<i class='fa fa-check'></i>">
      <div class="switch-off switch-animate">
        <input type="checkbox" checked>                                                           
        <span class="switch-left"><i class="fa fa-check"></i></span>
        <label>&nbsp;</label>
        <span class="switch-right"><i class="fa fa-times"></i></span>
      </div>
    </div>
    
    
    
    $(function () {
      $('.switch').click(function() {
        console.log(this);
        var CheckboxInput = $(this).find('input[type="checkbox"]');
        if ($(CheckboxInput).is(':checked')) {
          CheckboxInput.prop('checked','checked');
        }
      });
    });
    

    【讨论】:

    • 感谢@DPT 的回答。但这并不能解决使用 JS 切换属性的问题。不过,它最初会将 html 值设置为 true。
    • 嘿@Herm,你能再检查一次吗?我已经对其进行了编辑,并且工作正常。
    • @DPT...感谢更新。我已经更新了反对票。虽然还是不行
    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2019-09-30
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2018-08-08
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多