简体   繁体   English

当我的单选按钮选中时标签不会改变背景颜色

[英]Label not change background color when my radio button checked

Why my label box not change the color when the radio button checked?为什么我的标签框在选中单选按钮时不改变颜色? I also try input:checked+label or ~label{ syntax} but still no effect :我也尝试了 input:checked+label 或 ~label{ syntax} 但仍然没有效果:

 .ans{ display: none; } .answer input[type="radio"]:checked+label{ background-color: #FFA500; } .ans-list{ border-style:solid; border-width:thin; margin:20% auto; width:50%; } .ans-list:hover{ background-color:#D6D5A8; color:black; }
 <div class="answer an1"> <label class="ans-list" for="ans1">1</label><input type="radio" name="q1" class="ans false" id="ans1"><br> <label class="ans-list" for="ans2">4</label><input type="radio" name="q1" class="ans true" id="ans2"><br> <label class="ans-list" for="ans3">3</label><input type="radio" name="q1" class="ans false" id="ans3"><br> <label class="ans-list" for="ans4">2</label><input type="radio" name="q1" class="ans false" id="ans4"><br> <button type="submit" name="Submit" class="submit">Submit</button> <button type="button" class="next hide" name="next">Next</button> </div>

( Codepen ) 代码笔

I also tryed :我也试过:

 label{ display:block; margin: 1rem auto; border-style:solid; width:50%; } input[type="radio"]:checked+label{ background-color:red; color:red; }
 <label for="choice1"> <input type="radio" id="choice1" class="ans" name="op" checked="checked">male</label> <label for="choice2"> <input type="radio" id="choice2" class="ans" name="op">male</label> <label for="choice3"> <input type="radio" id="choice3" class="ans" name="op">male</label>

( Codepen ) 代码笔

Check this you need to change the structure 1st input after label there is no way to select direct previous siblings, with your code (+) you are selecting the next sibling.选中此项,您需要在label后更改结构 1st input无法直接选择先前的兄弟姐妹,使用您的代码 (+) 您正在选择下一个兄弟姐妹。 So you can just change the order of "label" and "input" and it will work所以你可以改变“标签”和“输入”的顺序,它会起作用

 .ans{ display: none; } .answer input[type="radio"]:checked+label{ background-color: #FFA500;} .ans-list{ border-style: solid; border-width: thin; margin: 0; width: 50px; height: 50px; display: flex; justify-content: center; align-items: center; } .ans-list:hover{ background-color:#D6D5A8; color:black; } button { margin: 25px 0; padding: 15px; display: inline-block; }
 <div class="answer an1"> <div class="answer-inner"> <input type="radio" name="q1" class="ans false" id="ans1"> <label class="ans-list" for="ans1">1</label> </div> <div class="answer-inner"> <input type="radio" name="q1" class="ans false" id="ans2"> <label class="ans-list" for="ans2">2</label> </div> <div class="answer-inner"> <input type="radio" name="q1" class="ans false" id="ans3"> <label class="ans-list" for="ans3">3</label> </div> <div class="answer-inner"> <input type="radio" name="q1" class="ans false" id="ans4"> <label class="ans-list" for="ans4">4</label> </div> <button type="submit" name="Submit" class="submit">Submit</button> <button type="button" class="next hide" name="next">Next</button> </div>

With CSS there is no way to select direct previous siblings, with your code (+) you are selecting the next sibling.使用 CSS 无法直接选择前一个兄弟姐妹,而使用您的代码 (+) 您正在选择下一个兄弟姐妹。 So you can just change the order of "label" and "input" and it will work, but it will change the order of the elements on the page.所以你可以改变“标签”和“输入”的顺序,它会起作用,但它会改变页面上元素的顺序。


Option 1选项1

The pure CSS solution to keep the layout untouched would be:保持布局不变的纯 CSS 解决方案是:

  • wrap label and button inside a flex div将标签和按钮包装在 flex div 中
  • swap label and input in HTML在 HTML 中交换标签和输入
  • flex-reverse this div flex-reverse这个div

Visually is the same, but now you can apply CSS selector视觉上是一样的,但现在你可以应用 CSS 选择器

<style>
.flex-rev {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-end;
}
</style>
<div class="flex-rev">
    <input type="radio" name="q1" class="ans false" id="ans1">
    <label class="ans-list" for="ans1">1</label>
</div>
<div class="flex-rev">
    <input type="radio" name="q2" class="ans false" id="ans2">
    <label class="ans-list" for="ans2">2</label>
</div>
[.....]

This work with pure CSS.这适用于纯 CSS。


Option 2选项 2

But, imho, the best option is adding a small js that add a class to the right label.但是,恕我直言,最好的选择是添加一个小 js,为正确的标签添加一个类。 It's slower but比较慢但是

  • you can defer it你可以推迟它
  • it doesn't change the layout and the structure of the page.它不会改变页面的布局和结构。

CSS part CSS 部分

<style type="text/css">
 
label.selected {
   background-color: #FFA500;
}
</style>

Javascript part Javascript 部分

<script type="text/javascript">
   // Get all inputs
   document.querySelectorAll('input[type="radio"]').forEach(function(i){
      // Add event listeners
      i.addEventListener('change', function(e){
         // when change, get all labels
         document.querySelectorAll('label').forEach(function(l){
            if (l.attributes.for.value == i.id) {
               // If for match with ID, add selected class
               l.classList.add('selected');
            } else { 
               // Clean others
               l.classList.remove('selected');
            }
         });         
      });
   })
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM