我有几个单选按钮,我正在添加一个类,点击时将突出显示的背景应用于项目标签。
现在,如果人们点击 radio ,它就会突出显示该标签。我也希望它在页面加载时突出显示。
如果选择了该 radio ,是否有任何方法可以在页面加载时突出显示标签?
$('.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/