2013-02-07 14 views
15

Próbuję ukryć element div, gdy użytkownik kliknie, pole wyboru i wyświetla je, gdy użytkownik usunie zaznaczenie tego pola wyboru. HTML:jQuery ukryj div, gdy pole wyboru jest zaznaczone, pokaż niezaznaczone

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

jQuery:

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

Mam trudny czas, aby dostać tę pracę.

+0

to pytanie może ci pomóc http://stackoverflow.com/questions/3312502/hide-text-when-checkbox-is-unchecked-using-jquery –

Odpowiedz

33

Pamiętaj, aby użyć zdarzenia ready.

Kod:

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

    }); 
}); 
+2

Wierzę, że to musi być problem, ponieważ kod działa dobrze na jsFiddle: http://jsfiddle.net/mxRCz/ –

+0

Okey, sprawdzę ponownie mój kod. – user2035638

+0

Znalazłem, gdzie miałem mój problem. Przyczyny tego problemu były tagi

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 
}); 
Powiązane problemy