Jquery 单选按钮选中时添加类,未选中时删除类

Jquery 单选按钮选中时添加类,未选中时删除类,jquery,css,Jquery,Css,你们能帮帮我吗。我想将类“checked”添加到带有类“.group_4”的跨度中。我想我应该使用jquery。我无法更改HTML 现在发生的情况是:当单击这两个项时,类不会被删除。因此,两个跨度都有“gechecked”类 有简单的解决办法吗 谢谢 $(文档).ready(函数(){ $(“.attribute_list label”)。单击(函数(){ 如果($('input.attribute_radio')。是(':checked')){ $(this.find('.group_4')

你们能帮帮我吗。我想将类“checked”添加到带有类“.group_4”的跨度中。我想我应该使用jquery。我无法更改HTML

现在发生的情况是:当单击这两个项时,类不会被删除。因此,两个跨度都有“gechecked”类

有简单的解决办法吗

谢谢

$(文档).ready(函数(){
$(“.attribute_list label”)。单击(函数(){
如果($('input.attribute_radio')。是(':checked')){
$(this.find('.group_4').addClass(“gechecked”);
}否则{
$(this.find('.group_4').removeClass(“gechecked”);
}
});
});

  • 格兰尼特
  • 镇静
这里是

请尝试以下代码:

    $(document).ready(function() {
      $(".attribute_list label").click(function() {
        $(this).find('.group_4').removeClass("gechecked");          
        $('input.attribute_radio:checked').parent().parent().parent().find('.group_4').addClass("gechecked");
      });
    });

首先从所有相关元素中删除该类,然后再将其设置为当前类…?如果您只有两个单选按钮,还可以尝试使用toggleClass:)@Jacob您的html与您的要求不匹配。您确定不能更改html删除该类似乎是解决方案。感谢虽然这个代码片段是受欢迎的,并且可能会提供一些帮助,但是它将是如何以及为什么解决这个问题的。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。
    $(document).ready(function() {
      $(".attribute_list label").click(function() {
        $(this).find('.group_4').removeClass("gechecked");          
        $('input.attribute_radio:checked').parent().parent().parent().find('.group_4').addClass("gechecked");
      });
    });
jQuery(document).ready(function($) {
  $(".attribute_list label").click(function() {
    $('.group_4').removeClass("gechecked");
    if ($('input.attribute_radio').is(':checked')) {
      $(this).find('.group_4').addClass("gechecked");
    }
  });
});