html - 单选按钮选中的选项

标签 html css radio-button

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

相关文章:

javascript - 在之前的下拉选择之后将值附加到单选按钮而不是下拉框

javascript - 一旦我检查了一种输入类型的 radio ,如何禁用和启用输入类型提交?

html - 如何向路由器发送格式不正确的 HTTP 请求?

javascript - 在不损失质量的情况下缩小 Konvajs 阶段

javascript - 根据另一个 div 的 ID 更改 div 上的背景图像

css - 这个 css 选择器有什么作用?

android - RadioGroup 中带有 TextView 的 RadioButtons

javascript - 如何返回类 <div> 的值而不返回 <span> 的值

javascript - 如何并排放置 2 个 div(替代解决方案)

javascript - 使用从 Instafeed.js 中提取的照片构建 Owl Carousel