【发布时间】:2016-11-23 06:21:28
【问题描述】:
我正在处理单选按钮,我从 google 帮助中设计了一些东西,但是在这个 fiddle 中,您会看到在页面加载时两者都被选中,我只使用了 radio1 按钮的选中选项。我需要将其更改为选中页面加载 radio1button 并且未选中 radio2 按钮。
当您单击第二个按钮时,您会看到按钮 1 未选中,但按钮 2 仍处于选中状态,有什么解决方案吗?
<input type="radio" name="radio" id="radio1" class="radio" checked/>
<label for="radio1">Now</label>
<input type="radio" name="radio" id="radio2" class="radio"/>
<label for="radio2">Later</label>
<style>
label {
width: 150px;
border-radius: 3px;
border: 1px solid #D1D3D4
}
/* hide input */
input.radio:empty {
margin-left: -999px;
}
/* style label */
input.radio:empty ~ label {
position: relative;
float: left;
line-height: 2.5em;
text-indent: 3.25em;
margin-top: 2em;
margin-left: 2em;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input.radio:empty ~ label:before {
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
content: '';
width: 2.5em;
background: #D1D3D4;
border-radius: 3px 0 0 3px;
}
/* toggle hover */
input.radio:hover:not(:checked) ~ label:before {
content:'\2714';
text-indent: .9em;
color: #C2C2C2;
}
input.radio:hover:not(:checked) ~ label {
color: #888;
}
/* toggle on */
input.radio:checked ~ label:before {
content:'\2714';
text-indent: .9em;
color: #9CE2AE;
background-color: #4DCB6D;
}
input.radio:checked ~ label {
color: #777;
}
/* radio focus */
input.radio:focus ~ label:before {
box-shadow: 0 0 0 3px #999;
}
</style>
【问题讨论】:
-
为什么不尝试将两个复选框都放在单独的 div 中可能对您有帮助。
标签: html css radio-button