Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2003
    Messaggi
    613

    [jQuery] checkbox bloccata

    Il problema che mi trovo a non saper risolvere è questo.
    Perché se clicco il div con classe "click-it" la checkbox si flagga ma se clicco la checkbox stessa lei non cambia il suo stato?
    Ho scritto questo filetto di esempio:

    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Prova</title>
    <script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" ></script>
    <script language="javascript">
    $(document).ready(function()
    {
    	$( ".click-it" ).toggle(function(){$(this).addClass("item-checked"); $(this).find('.check-it').attr('checked', true)},
    						 	function(){$(this).removeClass("item-checked"); $(this).find('.check-it').attr('checked', false)} )
    });
    </script>
    <style type="text/css">
    <!--
    .item-checked {
    	background-color: #FFD;
    }
    -->
    </style>
    </head>
    
    <body>
    <div class="click-it" style="border: 1px solid #CCC"><input class="check-it" type="checkbox" name="prova" value="" /></div>
    </body>
    </html>
    Conosci te stesso(?)
    (..e allora perchè scassi a me? )

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2003
    Messaggi
    613

    risolto

    Ho scoperto che con il Toggle questa cosa non funziona, occorre modificarlo così:

    codice:
    // Javascript
    $( ".click-it" ).click(function(){
        if($(this).hasClass("item-checked"))
       {
            $(this).removeClass("item-checked");
            $(this).find(':checkbox.check-it').attr('checked', false);
       }
       else
       {
            $(this).addClass("item-checked");
            $(this).find(':checkbox.check-it').attr('checked', true);
       }
    });
    Conosci te stesso(?)
    (..e allora perchè scassi a me? )

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    perche', come scritto qui
    The implementation also calls .preventDefault() on the event, so links will not be followed and buttons will not be clicked if .toggle() has been called on the element.

  4. #4
    Utente di HTML.it
    Registrato dal
    Jul 2003
    Messaggi
    613
    Non l'avevo letto,
    grazie.
    Conosci te stesso(?)
    (..e allora perchè scassi a me? )

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.