2013-02-07 38 views
15

Tôi đang cố ẩn div khi người dùng nhấp vào hộp kiểm và hiển thị nó khi người dùng bỏ chọn hộp kiểm đó. HTML:jQuery ẩn div khi hộp kiểm được chọn, hiển thị trên không được chọn

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

jQuery:

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

Tôi gặp một thời gian khó khăn để làm việc này.

+0

câu hỏi này có thể giúp bạn http://stackoverflow.com/questions/3312502/hide-text-when-checkbox-is-unchecked-using-jquery –

Trả lời

33

Đảm bảo sử dụng sự kiện ready.

Code:

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

    }); 
}); 
+2

Tôi tin rằng đây là vấn đề, vì mã hoạt động tốt jsFiddle: http://jsfiddle.net/mxRCz/ –

+0

Okey, tôi sẽ kiểm tra lại mã của mình. – user2035638

+0

Tôi đã tìm thấy nơi tôi gặp sự cố.

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 
}); 
Các vấn đề liên quan