css 自定义收音机选择器不工作

vyswwuz2  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(157)

我已为我的站点创建了此自定义单选选择器,第一个单选选择器正常工作,但第二个字段未选择。是否需要创建另一个类,或者是否需要为每个输入命名不同的ID,以便它们不会相互冲突?我正在尝试创建多个选项,以便客户选择一个选项。

input[type=radio] {
  position: absolute;
  visibility: hidden;
  display: none;
}

label {
  display: inline-block;
  cursor: pointer;
  font-weight: bold;
  padding: 5px 20px;
}

input[type=radio]:checked+label {
  color: #fff;
  background: #444;
  border-radius: 5px;
}

label+input[type=radio]+label {
  border-left: solid 3px #444;
}

.radio-group {
  display: inline-block;
  overflow: hidden;
}
<div class="radio-group">
  <input type="radio" id="lessthan6months" name="timeline"><label for="lessthan6months">6 mo</label>
  <input type="radio" id="6to12months" name="timeline"><label for="6to12months">6-12 mo</label>
  <input type="radio" id="12to24months" name="timeline"><label for="12to24months">12-24 mo</label>
  <div class="line"></div>
</div>

<div class="radio-group">
  <input type="radio" id="tradein_y" name="tradein" value="Yes"><label for="trade in yes">Yes</label>
  <input type="radio" id="tradein_n" name="tradein" value="No"><label for="trade in no">No</label>
  <div class="line"></div>
</div>
v1l68za4

v1l68za41#

label内容的for必须与inputid匹配。
这样就行了:

input[type=radio] {
  position: absolute;
  visibility: hidden;
  display: none;
}

label {
  display: inline-block;
  cursor: pointer;
  font-weight: bold;
  padding: 5px 20px;
}

input[type=radio]:checked+label {
  color: #fff;
  background: #444;
  border-radius: 5px;
}

label+input[type=radio]+label {
  border-left: solid 3px #444;
}

.radio-group {
  display: inline-block;
  overflow: hidden;
}
<div class="radio-group">
  <input type="radio" id="lessthan6months" name="timeline"><label for="lessthan6months">6 mo</label>
  <input type="radio" id="6to12months" name="timeline"><label for="6to12months">6-12 mo</label>
  <input type="radio" id="12to24months" name="timeline"><label for="12to24months">12-24 mo</label>
  <div class="line"></div>
</div>

<div class="radio-group">
  <input type="radio" id="tradein_y" name="tradein" value="Yes"><label for="tradein_y">Yes</label>
  <input type="radio" id="tradein_n" name="tradein" value="No"><label for="tradein_n">No</label>
  <div class="line"></div>
</div>

相关问题