简体   繁体   English

单击时永久更改单选按钮的颜色

[英]Change color of a Radio button permanently on click

Hi I designed a serious of Radio buttons. 嗨,我设计了一个严重的单选按钮。 I want to change the color of radio buttons permanently on click. 我想在单击时永久更改单选按钮的颜色。 Please help me to implement this design. 请帮助我实现此设计。 I have written this code here, Which is not working. 我已经在这里编写了此代码,但无法正常工作。 Any suggestions would be highly appreciated. 任何建议将不胜感激。

 $('.btn.btn-default').on("click", function() { //$('button').not(this).removeClass(); $(this).toggleClass('active'); }); 
 .active { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="btn-group btn-group-justified" data-toggle="buttons"> <label class="btn btn-default"> <input type="radio" name="options" id="option1" autocomplete="off" checked> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option2" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option3" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option4" autocomplete="off"> </label> </div> </div> </div> 

You can use add and remove class with siblings like this. 您可以使用具有此类的同级添加和删除类。

 $('.btn.btn-default').on("click", function() { $(this).addClass('active'); $(this).siblings().removeClass('active') }); 
 .active { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="btn-group btn-group-justified" data-toggle="buttons"> <label class="btn btn-default"> <input type="radio" name="options" id="option1" autocomplete="off" checked> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option2" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option3" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option4" autocomplete="off"> </label> </div> </div> </div> 

I think you only need to change the toggleClass with addClass as you need to change color permanently. 我认为您只需要用addClass更改toggleClass ,因为您需要永久更改颜色。

$('.btn.btn-default').on("click", function() {
  $(this).addClass('active');
});

Try this code, Its working 试试这个代码,它的工作原理

 $('.btn.btn-default').on("click", function() { //$('button').not(this).removeClass(); $(this).addClass('active'); }); 
 .active { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="btn-group btn-group-justified" data-toggle="buttons"> <label class="btn btn-default"> <input type="radio" name="options" id="option1" autocomplete="off" checked> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option2" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option3" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option4" autocomplete="off"> </label> </div> </div> </div> 

In my case, the below solution worked the best. 就我而言,以下解决方案效果最佳。 Thanks all for your valuable inputs. 感谢大家的宝贵意见。

 <style>
     .btn-default.active{background-color:blue;}
  </style>

  <script>
     $('.btn.btn-default').on("click",function(){  
       //$('button').not(this).removeClass();
       $(this).addClass('active');

     });
  </script>

I added .btn-default CSS class to the style. 我在样式中添加了.btn-default CSS类。

You just need to add JQuery after loading the whole body. 您只需要在加载整个主体后添加JQuery。

I think this will help you. 我认为这会对您有所帮助。

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <style> .active{background-color:red;} </style> <div class="container"> <div class="row"> <div class="btn-group btn-group-justified" data-toggle="buttons"> <label class="btn btn-default"> <input type="radio" name="options" id="option1" autocomplete="off" checked> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option2" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option3" autocomplete="off"> </label> <label class="btn btn-default"> <input type="radio" name="options" id="option4" autocomplete="off"> </label> </div> </div> </div> <script> $('.btn.btn-default').on("click",function(){ //$('button').not(this).removeClass(); $(this).toggleClass('active'); }); </script> </body> </html> 

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

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