简体   繁体   English

选中/取消选中单选按钮

[英]Make radio buttons checked /unchecked

 $("input[type='radio']").each(function() { if ($(this).is(":checked")) { $(this).css('background', 'blue'); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="radio" data="cool" name="cool" checked="checked"> <input type="radio" data="cool" name="cool"> <input type="radio" data="cool" name="cool"> 

My approach is to first check if my inputs are :checked and if they are, put some CSS class with the background color. 我的方法是先检查输入是否经过:checked然后输入一些CSS类作为背景色。 I achieve that, the next thing I want to is to remove that :checked when users click on radio button or any other (better) idea. 我实现了这一点,接下来我想删除的是:checked当用户单击单选按钮或任何其他(更好的)主意时, :checked该选项。 After the form is submitted, this code checks if inputs are :checked , the problem is when I want to select another radio button I get something like this: 提交表单后,此代码检查输入是否为:checked ,问题是当我想选择另一个单选按钮时,我得到如下信息:

忙碌的猫

1 and 2 radio buttons are selected, it should be only 2 :checked 已选择1和2单选按钮,应仅:checked 2 :checked

You need to add the else to remove the blue color like : 您需要添加else来删除蓝色,例如:

$("input[type='radio']").each(function () {
   if ($(this).is(":checked")) {
      $(this).css('background', 'blue');
   }else{
     $(this).css('background', 'white');
   }
});

You could also attach a click event for those radios like : 您还可以为这些收音机添加点击事件,例如:

$("body").on("click", "input[type='radio']", function () {
    $("input[type='radio']").css('background', 'white');
    $(this).css('background', 'blue');
});

The issue with your JS is that you never remove the class from any of the unselected checkboxes. JS的问题在于,您永远不会从任何未选中的复选框中删除该类。 Also note that each() only runs when the page loads (assuming you've not placed it in an event handler, but the question doesn't show that), so you need to instead run your logic in a change event handler: 还要注意, each()仅在页面加载时运行(假设您尚未将其放置在事件处理程序中,但问题并未显示出来),因此您需要在change事件处理程序中运行逻辑:

var $radio = $("input[type='radio']").on('change', function() {
  $radio.removeClass('blue');
  $(this).toggleClass('blue', this.checked);
});

That being said, what you're trying to do can be achieved more simply by using CSS: 话虽如此,通过CSS可以更轻松地实现您要执行的操作:

 input { visibility: hidden; } input:before { content: ''; position: absolute; width: 20px; height: 20px; border-radius: 50%; background-color: #CCC; visibility: visible; } input:checked:before { background-color: blue; } 
 <input type="radio" data="cool" name="cool" checked="checked"> <input type="radio" data="cool" name="cool"> <input type="radio" data="cool" name="cool"> 

I think the issue with your code is that you are using each event instead of change or click event. 我认为您的代码存在的问题是您使用的是each事件,而不是changeclick事件。 It means that you are trying to change the color of your radio button, even before user has performed any action. 这意味着您甚至在用户未执行任何操作之前都在尝试更改单选按钮的颜色。 Read the following code, this will solve the issue of submitting the form and also customizing the radio button: 阅读以下代码,这将解决提交表单和自定义单选按钮的问题:

 $(".radio-button").click(function() { $(this).addClass("blue-background"); $(this).siblings().removeClass("blue-background"); var radioID = $(this).data('radio'); $('#' + radioID).attr("checked", "checked"); if ($('#' + radioID).siblings(":checked")) { $('#' + radioID).siblings().removeAttr("checked"); } }); 
 .blue-background { background-color: blue; } input[type='radio'] { visibility: hidden; } .radio-button { height: 15px; width: 15px; margin: 5px; border-radius: 50%; display: inline-block; border: 1px solid black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="parent"> <input type="radio" id="radio1" data="cool" name="cool" checked="checked"> <input type="radio" id="radio2" data="cool" name="cool"> <input type="radio" id="radio3" data="cool" name="cool"> <div class="radio-button" data-radio="radio1"></div> <div class="radio-button" data-radio="radio2"></div> <div class="radio-button" data-radio="radio3"></div> </div> 

I hope this was helpful. 我希望这可以帮到你。

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

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