[英]jQuery add class to radio box not removed
如果選中,我使用jQuery將類添加到單選框中。 這工作正常,但在選擇另一個無線電盒時它沒有刪除該類。
我錯過了什么?
jQuery(".checkbox-radio").change(function() {
if ($(this).is(':checked'))
$(this).closest(".radio-inline").addClass("selected");
else
$(this).closest(".radio-inline").removeClass("selected");
});
<label class="radio-inline conf-option-1">
<input type="radio" class="checkbox-radio" name="configoption[1]" value="1" checked="checked" onclick="prodconfrecalcsummary()">
<i class="icon-addon"></i>
<span class="addon-qty"></span>
<span class="addon-text">Text</span>
<span class="addon-price"> Text </span>
</label>
<label class="radio-inline conf-option-2">
<input type="radio" class="checkbox-radio" name="configoption[2]" value="2" checked="checked" onclick="prodconfrecalcsummary()">
<i class="icon-addon"></i>
<span class="addon-qty"></span>
<span class="addon-text">Text</span>
<span class="addon-price"> Text </span>
</label>
<label class="radio-inline conf-option-3">
<input type="radio" class="checkbox-radio" name="configoption[3]" value="3" checked="checked" onclick="prodconfrecalcsummary()">
<i class="icon-addon"></i>
<span class="addon-qty"></span>
<span class="addon-text">Text</span>
<span class="addon-price"> Text </span>
</label>
刪除selected
從所有類.radio-inline
,然后添加/刪除selected
為每個條件,如下所示。
<script>
jQuery(".checkbox-radio").change(function() {
$(".radio-inline").removeClass("selected"); //add this
if ($(this).is(':checked')){
$(this).closest(".radio-inline").addClass("selected");
}
else
$(this).closest(".radio-inline").removeClass("selected");
});
</script>
The name attribute should be the same for all your check-boxes
See here:
jQuery(".checkbox-radio").on('click',function() {
if ($(this).is(':checked'))
$(this).closest(".radio-inline").addClass("selected");
else
$(this).closest(".radio-inline").removeClass("selected");
});
像這樣使用
$(".radio-inline").click(function() {
$(".radio-inline").find("input").removeClass("selected");
if ($(this).find("input").is(':checked')){
$(this).find("input").addClass("selected");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.