How do I add a class to the container div of a checkbox when the checkbox is checked on and off?

≡放荡痞女 提交于 2020-01-06 07:48:44

问题


I want to add a background color to the container(div.checkbox_db), when the checkbox(input.check-group) is activated using jquery. Can I do this..? Here is my html code I am working with:

<div class="grid_3 checkbox_db off-check">

    <input type="checkbox" class="check-group" id="check-1" name="check-1">

    <label for="check-1" class="group">Checkbox</label>

</div>

回答1:


$('.check-group').click(function(){
  if($(this).is(':checked')){
    $(this).parent().addClass('some_class');
  }else{
    $(this).parent().removeClass('some_class');
  }
});



回答2:


I think it is what you want:

javascript

$(".grid_3").removeClass('off-check');
    $('.check-group').click(function(){
    if($(this).is(':checked')){
        $(this).parent().addClass('off-check');
    }
    else {
        $(this).parent().removeClass('off-check');
    }
});

stylesheet

.off-check{
 background-color:yellow;  
}

Demo here: http://jsfiddle.net/kxFCd/



来源:https://stackoverflow.com/questions/11068446/how-do-i-add-a-class-to-the-container-div-of-a-checkbox-when-the-checkbox-is-che

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!