【问题标题】:Custom Radio Buttons: How to uncheck? (Checkbox Hack) [duplicate]自定义单选按钮:如何取消选中? (复选框黑客)[重复]
【发布时间】:2017-12-07 16:48:52
【问题描述】:

我创建了一些效果很好的自定义复选框:

https://codepen.io/anon/pen/jwxXar

我尝试对单选按钮做同样的事情。但是,如果一个单选按钮处于活动状态并且我单击了另一个单选按钮,则取消选中不起作用

https://codepen.io/anon/pen/YQLdxd

自定义单选按钮 CSS 代码

.radio  {
    position: relative;
    margin-bottom: 10px;
}

.radio input[type="radio"] {
    cursor: pointer;
    height: 100%;
    left: 0;
    margin: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 1;
}

.radio label {margin-left: 32px;}

.radio label:before,
.radio label:after {
    content: "";
    display: block;
    position: absolute;
}

.radio label:before {
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 50%;
    height: 20px;
    left: 0;
    top: 4px;
    width: 20px;
}

.radio label:after {
    background: red;
    border-radius: 50%;
    height: 12px;
    left: 5px;
    opacity: 0;
    pointer-events: none;
    top: 9px;
    width: 12px;
}

.radio input:checked ~ label:after {opacity: 1;}

input[type="radio"]:checked + label:before {
    border: 1px solid red;
    box-shadow: inset 0 0 0 1px #fff, inset 0 0 0 0 #fff, inset 0 0 0 16px #fff;
    color: #fff;
}

自定义单选按钮 HTMLCode

<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 1</label>
</div>
<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 2</label>
</div>
<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 3</label>
</div>
<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 4</label>
</div>

有没有任何 CSS 超级英雄知道,为什么单选按钮不起作用?

【问题讨论】:

  • 您必须指定收音机的名称:name="radios",该组所有收音机的名称相同
  • 他们需要一个name 属性(所有名称都相同)

标签: css radio-button css-hack


【解决方案1】:

您必须指定无线电的名称:name="radios",该组的所有无线电都使用相同的名称

.radio  {
    position: relative;
    margin-bottom: 10px;
}

.radio input[type="radio"] {
    cursor: pointer;
    height: 100%;
    left: 0;
    margin: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 1;
}

.radio label {margin-left: 32px;}

.radio label:before,
.radio label:after {
    content: "";
    display: block;
    position: absolute;
}

.radio label:before {
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 50%;
    height: 20px;
    left: 0;
    top: 4px;
    width: 20px;
}

.radio label:after {
    background: red;
    border-radius: 50%;
    height: 12px;
    left: 5px;
    opacity: 0;
    pointer-events: none;
    top: 9px;
    width: 12px;
}

.radio input:checked ~ label:after {opacity: 1;}


input[type="radio"]:checked + label:before {
    border: 1px solid red;
    box-shadow: inset 0 0 0 1px #fff, inset 0 0 0 0 #fff, inset 0 0 0 16px #fff;
    color: #fff;
}
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 1</label>
</div>
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 2</label>
</div>
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 3</label>
</div>
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 4</label>
</div>

【讨论】:

  • 是的,就是这样。我忘记了name attribute。非常感谢!
【解决方案2】:

您的 CSS 没有问题。对于单选,您唯一需要注意的是,单选按钮始终成组工作。所以你必须给他们起名字 (name attribute)

<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 1</label>
</div>
<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 2</label>
</div>
<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 3</label>
</div>
<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 4</label>
</div>

【讨论】:

  • 你是对的。 name attribute 不见了。谢谢!
猜你喜欢
  • 2012-01-07
  • 2014-09-24
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 2017-09-28
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多