选中复选框时,jQuery隐藏div,未选中时显示

时间:2013-02-07 17:58:53

标签: javascript jquery html checkbox

我试图在用户点击复选框时隐藏div,并在用户取消选中该复选框时显示该div。 HTML:

<div id="autoUpdate" class="autoUpdate">
   content
</div>

jQuery的:

<script>
$('#checkbox1').change(function(){
        if (this.checked) {
            $('#autoUpdate').fadeIn('slow');
        }
        else {
            $('#autoUpdate').fadeOut('slow');
        }                   
    });
</script>

我很难让这个工作。

2 个答案:

答案 0 :(得分:40)

请务必使用ready事件。

<强>代码:

$(document).ready(function(){
    $('#checkbox1').change(function(){
        if(this.checked)
            $('#autoUpdate').fadeIn('slow');
        else
            $('#autoUpdate').fadeOut('slow');

    });
});

答案 1 :(得分:1)

HTML

<input type="checkbox" id="cbxShowHide"/><label for="cbxShowHide">Show/Hide</label>
<div id="block">Some text here</div>

CSS

#block{display:none;background:#eef;padding:10px;text-align:center;}

javascript / jquery

$('#cbxShowHide').click(function(){
this.checked?$('#block').show(1000):$('#block').hide(1000); //time for show
});
相关问题