【问题标题】:Mutually exclusive checkboxes which are styled with :before and :after CSS互斥复选框,样式为 :before 和 :after CSS
【发布时间】:2014-05-23 04:21:28
【问题描述】:

老读者,第一次发帖。

我正在使用 http://proto.io/freebies/ 中设置为 ON/OFF 开关样式的复选框

这对于“复选框”适用的情况非常有效。但是,我现在遇到了一种情况,我需要类似“无线电”的功能(互斥)。

我找到了一些用于制作互斥复选框的 Jquery 解决方案,但它们不适应 Proto.io 开关的 :before 和 :after CSS 样式。

如何使 div class="checkrow" 中的样式复选框互斥?

JS 小提琴:http://jsfiddle.net/zLGnu/2/

<div class="checkrow">
<div class="w-col w-col-3">
    <div class="onoffswitch">
        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="instagram">
        <label class="onoffswitch-label" for="instagram">
            <div class="onoffswitch-inner-instagram"></div>
            <div class="onoffswitch-switch"></div>
        </label>
    </div>
</div>
<div class="w-col w-col-3">
    <div class="onoffswitch">
        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="twitter">
        <label class="onoffswitch-label" for="twitter">
            <div class="onoffswitch-inner-twitter"></div>
            <div class="onoffswitch-switch"></div>
        </label>
    </div>
</div>

【问题讨论】:

标签: javascript jquery html css checkbox


【解决方案1】:

快速修复,

$(document).ready(function () {
    $('.onoffswitch-label').click(function () {
        $(".onoffswitch-checkbox").parent().removeClass('onoffswitch-checked');
        $(this).parent().addClass('onoffswitch-checked');
    });
});

http://jsfiddle.net/zLGnu/3/

注意:更改输入中的type="radio"

【讨论】:

  • 感谢您的帮助!这确实有效,但它并不是我想要的。我需要这些开关能够被分组并与同一组中的那些开关互斥。
  • 我的样本是互斥的。如果你想分组,jsfiddle.net/zLGnu/5你可以给任何组名。
  • 我最终选择了这个:jsfiddle.net/zLGnu/8 虽然我对面向对象编程知之甚少,但这似乎更面向对象,因为它更容易应用于许多情况而无需命名组或编写每组一个新功能。感谢您的帮助,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2011-08-17
  • 2018-01-02
相关资源
最近更新 更多