[removed] Change label background color on checkbox

后端 未结 7 1460
执笔经年
执笔经年 2021-01-25 06:37

I am trying to change the background color of a label in each checkbox in a form based on the checked/unchecked state of the checkbox.So far I got it to change initially, but it

7条回答
  •  猫巷女王i
    2021-01-25 06:54

    Your are not setting the color on the label but on the checkbox. jQuery makes it easy for you to select / traverse DOM elements (and also helps clean-up a lot of unnecessary IDs), see this fiddle :

    http://jsfiddle.net/7wnCL/17/

    $('input[type=checkbox]').change(function(){
        if($(this).prop('checked')){   
            $(this).parent().css('backgroundColor', '#bff0a1');
        }else{
            $(this).parent().css('backgroundColor', '#eee');        
        }
    });
    

提交回复
热议问题