Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2004
    Messaggi
    1,344

    jQuery: condizione su submit di form

    Ho questo form dal quale devo selezionare almeno un checkbox e di cui faccio il submit tramite un link. Vorrei però che il submit avenisse esclusivamente se almeno uno dei checkbox sia selezionato:

    Codice PHP:
                        <form class="form" method="post" name="frm_ospiti" id="frm_ospiti" action="ospiti_registrazione.php">
                        
                        <
    table class="table" cellspacing="0" width="100%">
                                
                            <
    thead>
                                <
    tr>
                                    <
    th class="black-cell"><span class="loading"></span></th>
                                    <
    th scope="col">
                                        
    Azienda
                                    
    </th>
                                    <
    th scope="col">

                                        
    Ospite
                                    
    </th>
                                </
    tr>
                            </
    thead>
                            
                            <
    tbody>
                                <
    tr>
                                                <
    th scope="row" class="table-check-cell"><input type="checkbox" name="selected[]" value="3"></th>
                                                <
    td>Eurogroup</td>
                                                <
    td>Covassin Denis</td>

                                            </
    tr><tr>
                                                <
    th scope="row" class="table-check-cell"><input type="checkbox" name="selected[]" value="4"></th>
                                                <
    td>Eurogroup</td>
                                                <
    td>Della Silvestra Andrea</td>
                                            </
    tr><tr>
                                                <
    th scope="row" class="table-check-cell"><input type="checkbox" name="selected[]" value="1"></th>
                                                <
    td>Idealogica</td>

                                                <
    td>Ciccone Alessandro</td>
                                            </
    tr><tr>
                                                <
    th scope="row" class="table-check-cell"><input type="checkbox" name="selected[]" value="5"></th>
                                                <
    td>SMC</td>
                                                <
    td>Andrea Cisolla</td>
                                            </
    tr><tr>
                                                <
    th scope="row" class="table-check-cell"><input type="checkbox" name="selected[]" value="2"></th>

                                                <
    td>SMC</td>
                                                <
    td>Fabrizio Piccolo</td>
                                            </
    tr>                        </tbody>
                        </
    table>
                        
                        </
    div>
                        
                        <
    div class="block-footer no-margin">
                            [
    img]../images/icons/fugue/arrow-curve-000-left.png[/img
                            [
    url="javascript:void(0);"]Tutti[/url]

                            [
    url="javascript:void(0);"]Nessuno[/url]
                            <
    span class="sep"></span>
                            [
    url="javascript:void(0);"][img]../images/icons/fugue/tick-circle.png[/imgRegistra[/url]
                        </
    div>
                        </
    form
    Questo il javascript per attivare i pulsanti:

    Codice PHP:
    <script type="text/javascript">
        
        $(
    document).ready(function() {
            
            $(
    '#btn_seleziona').click(function() {
                $(
    "th.table-check-cell :checkbox:enabled").attr({ checked"checked" });
            });
            
            $(
    '#btn_deseleziona').click(function() {
                $(
    "th.table-check-cell :checkbox:checked").attr({ checked"" });
            });
            
            $(
    '#btn_registra').click(function() {
                $(
    '#frm_ospiti').submit();
            });
        
        });

    </script> 
    In pratica nella parte del #btn_registra dovrei permettere il submit solo nel caso in cui almeno un checkbox sia selezionato, ma non ho idea di come fare...

  2. #2
    Allura premetto che non so se può andare, cioè io ho provato, funziona, bisogna vedere se t da un'output corretto in ogni caso:

    codice:
    <script type="text/javascript">
        
        $(document).ready(function() {
            
            $('#btn_seleziona').click(function() {
                $("th.table-check-cell :checkbox:enabled").attr({ checked: "checked" });
            });
            
            $('#btn_deseleziona').click(function() {
                $("th.table-check-cell :checkbox:checked").attr({ checked: "" });
            });
            
            $('#btn_registra').click(function() {
    										  if($('th.table-check-cell :checkbox:checked').length > 0) {
    											   $('#frm_ospiti').submit();
    										  } else {
    											  alert("devi selezionare un'elemento per poter inviare il form");
    										  }
            });
        
        });
    
    </script>
    In sostanza conta quanti checkbox sono ATTIVI se non vi è nessun checkbox attivo non invia il form... E ti fa vedere un'alert, quest'ultimo puoi anche cambiarlo con un dialog o quello che te pare... in jquery.

    io ho anche levato questo:
    faceldolo diventare:

    Non so a cosa serve quella riga di JS ma a me dava errore e l'ho eliminata.

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2004
    Messaggi
    1,344
    Grazie.

    Ti dava errore perchè l'editor l'ha trasformata in java-script:void(0); mentre in originale è:

    javascript:void(0);

    e viene utilizzato in modo che il link non effettui nessuna operazione. Con il cancelletto se hai una pagina lunga al click ti rimanda al top della pagina ed è fastidioso.

    Equivale al return false;

  4. #4
    Prego.

    Grazie per la dritta

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.