当使用jquery选中复选框时,是否可以更改div的背景

时间:2014-07-21 13:32:54

标签: jquery checkbox

当使用jquery选中复选框时,是否可以更改div的红色背景。 示例fiddle

<div class="col-md-4 wizardBox">
<label for="img1">
    <img src="http://beststore.bugs3.com/img/sample/portfolio-macbook.fw.png" />
    <p>Culture</p>
    <input type="checkbox" class="img1" id="img1" name="img1" value="" />
</label>
</div>   

4 个答案:

答案 0 :(得分:1)

您可以使用:

$('#img1').change(function() {
    if($(this).is(":checked")) 
       $('.wizardBox').css('background-color','red');
    else
       $('.wizardBox').css('background-color','');
});

<强> Working Demo

答案 1 :(得分:0)

是的...只需在复选框上收听更改事件,然后更改背景。

答案 2 :(得分:0)

尝试

$('#img1').click(function() {
    if($(this).is(":checked")) 
       $(this).closest(".wizardBox").css('background-color','red');
});

答案 3 :(得分:0)

嗨,最好将控件缓存在变量

//here we getting the control(div) in the variable 
var $div=$('.wizardBox');
$('#img1').change(function() {
    if($(this).is(":checked")) 
       $div.css('background-color','red');
    else
       $div.css('background-color','');
});

我已经为您更新了JSFiddle click here to see