gpt4 book ai didi

html - 单选按钮选中的选项

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:06 26 4
gpt4 key购买 nike

我正在研究单选按钮,我从谷歌帮助中设计了一些东西,但在这个 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>

最佳答案

代替一般的兄弟~使用相邻兄弟+选择器

一般兄弟会选择元素旁边的所有标签

相邻的兄弟只会选择下一个标签

https://jsfiddle.net/victor_007/8ax050s8/1/

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;
}
<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>

关于html - 单选按钮选中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com