我有几个单选按钮,我要添加一个类,单击该类将突出显示的背景应用于项目标签。

现在,我可以在人们单击收音机的地方突出显示该标签。我也希望页面加载时突出显示它。

如果选择了该收音机,是否可以在页面加载时突出显示标签?

$('.subscription-option input').click(function() {
    $('label').removeClass("highlight-this");
    if($(this).is(':checked')) {
        $(this).parent().addClass('highlight-this');
    }
});


页面:https://www.solonutrition.com/product/dark-chocolate-mandarin/

最佳答案

尝试不同的方法:



var paintLabel = function paintLabel()
{
  $('label').removeClass("highlight-this");
  $('input:checked').parent().addClass('highlight-this');
};

$('input').click(paintLabel);

paintLabel();

.highlight-this {
  background-color: #aa0
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label>
  <input type="radio" name="rdb"> Rdb1
</label>
<label>
  <input type="radio" name="rdb" checked="checked"> Rdb2
</label>
<label>
  <input type="radio" name="rdb"> Rdb3
</label>

关于javascript - 单选按钮-初始检查并单击检查,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54467581/

10-12 07:29
查看更多