【问题标题】:how to uncheck all Toggle Switch when one toggle switch is checked?选中一个拨动开关时如何取消选中所有拨动开关?
【发布时间】:2019-07-09 08:43:04
【问题描述】:

当我选中一个切换开关时,谁能告诉我如何将其他切换开关更改为未选中状态。我可以使用复选框,但我发现使用 Javascript Toggle Switch 非常棘手

        if (this.checked) {
          $(":checkbox[value=switch-intermediate]").removeAttr("checked", null);
          $(":checkbox[value=switch-expert]").removeAttr("Checked",null);
        }

我上面的代码适用于复选框,但不适用于切换开关。我确实在网上看到了一些其他的例子,比如这个 - Uncheck or turn off all checkbox based toggle switches when a new one is turned on? 但是当我遵循它时,它仍然不起作用。谢谢!

[jfiddle] (https://jsfiddle.net/jt100/4xjf1ano/3/)

【问题讨论】:

  • 声明当前复选框,let selected = this; 然后在您的 if 语句中进行选择,但也选择 .not() 包含选定的复选框。 $(":checkbox[value=switch-intermediate], :checkbox[value=switch-expert]").not(selected).removeAttr("checked", null); --- api.jquery.com/not
  • 我试试你的方法,还是不行。这就是我尝试过的 - jsfiddle.net/jt100/4xjf1ano/25

标签: javascript jquery


【解决方案1】:

使用简单的change 事件和.not(this)

  • 使用$(".switch:not([checked])")无需检查if(this.checked)
  • 要更改checked/unchecked,请使用.prop("checked" , true/false)
  • 为了防止this复选框在其他人未选中时未选中,请使用.not(this)

$(".switch:not([checked])").on('change' , function(){
  $(".switch").not(this).prop("checked" , false);
});
.switch-label {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: #5c13ec;
}

input:focus + .slider {
  box-shadow: 0 0 1px #5c13ec;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="toggle-two">
  <div>Novice</div>
  <label class="switch-label">
    <input class="switch" id="switch-novice" value="switch-novice"  type="checkbox"/>
     <span class="slider round"></span>
  </label>
</div>

<div class="toggle-three">
  <div>Intermediate</div>
  <label class="switch-label">
    <input class="switch" id="switch-intermediate" value="switch-intermediate"  type="checkbox"/>
    <span class="slider round"></span>
  </label>
</div>

<div class="toggle-four">
  <div>Expert</div>
  <label class="switch-label">
    <input class="switch" id="switch-expert" value="switch-expert" type="checkbox" />
    <span class="slider round"></span>
  </label>
</div>
  • 另外,如果你在其他地方有更多 switch .. 你可以使用 $(this).closest('CONTAINER').find('.switch').not(this)........ 而不是 $('.switch').not(this)..... SEE Example HERE

【讨论】:

  • 谢谢@CodeNinja,不客气..另外,如果您在其他地方有更多switch ..您可以使用$(this).parent().find('.switch').not(this)..........祝您有美好的一天:)
  • NVM @CodeNinja 不客气.. 但是 你需要知道这里的所有演示作品都应该在真实的网站上工作,然后你可以和发帖的人交谈答案和寻求帮助 .. 简单易行 ;) .. 祝你有美好的一天 :-)
  • @CodeNinja 对.parent() 感到抱歉,应该是.closest() .. 用示例查看我的更新答案
【解决方案2】:

将输入从复选框更改为单选按钮,并为每个单选按钮添加name="something",在所有相关按钮上使用相同的“某事”名称。

基本上,每个输入都会从这个变化

<input class="switch" id="switch-novice"
       value="switch-novice" type="checkbox" />

到这里

<input class="switch" id="switch-novice"
       value="switch-novice" type="radio" name="switch-choice" />

我已经更新了你的fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多