Visualizzazione dei risultati da 1 a 5 su 5

Discussione: 2 Funzioni su checkbox

  1. #1

    2 Funzioni su checkbox

    Salve a tutti, ho una funzione "f(frmObj)" che fa si che quando si seleziona un checkbox venga aggiunto il suo valore ad un campo di testo "who" (ogni valore è separato da un trattino "-").
    Ho aggiunto però una funzione "check(field)" che permette di selezionare e deselezionare tutti i checkbox premendo un solo pulsante...
    Vorrei fare in modo di richiamare la funzione "f(frmObj)" nella funzione "f(frmObj)"...
    Cioè nell'esempio:
    1)Al caricamento della pagina poichè tutte le checkbox sono selezionate, vorrei che "who" abbia il valore di tutte le checkbox.
    2)Al "Seleziona Tutti" vorrei che "who" abbia il valore di tutte le checkbox.
    3)Al "Deseleziona Tutti" vorrei che "who" abbia il valore "" (vuoto).

    Questo è il codice che ho attualmente:
    codice:
    <SCRIPT LANGUAGE="JavaScript">
    var checkflag = "true";
    function check(field) {
    if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
    field[i].checked = true;}
    checkflag = "true";
    return "Deseleziona Tutti"; }
    else {
    for (i = 0; i < field.length; i++) {
    field[i].checked = false;}
    checkflag = "false";
    return "Seleziona Tutti"; }
    }
    
    function f(frmObj)
    {
     
    var strVal = "";
    var el = frmObj.elements;
    var nFields = el.length;
    var sepStr = "-";
     
    for ( n = 0 ; n < nFields ; n++ )
    {
    if (el[n].type == "checkbox" && el[n].checked)
    {
    if (strVal.length > 0) strVal += sepStr;
    strVal += el[n].value;
    }
    }
    document.form1.who.value = strVal;
    }
    </script>
    
    <form name="form1" method="post" action="test.php">
    <input type=button value="Deseleziona Tutti" onClick="this.value=check(this.form.list)"> 
    
    
    <input type=checkbox name=list value="1" checked onClick="f(this.form);">Opzione 1
    
    <input type=checkbox name=list value="2" checked onClick="f(this.form);">Opzione 2
    
    <input type=checkbox name=list value="3" checked onClick="f(this.form);">Opzione 3
    
    <input type=checkbox name=list value="4" checked onClick="f(this.form);">Opzione 4
    
    <input type=checkbox name=list value="5" checked onClick="f(this.form);">Opzione 5
    
    
    <input type=text name=who>
    
    <input type="submit" name="Submit" value="Invia">
    
    </form>
    Mi date una mano per favore?
    Vi ringrazio tutti in anticipo!
    Ciao!
    What is the |\/|atrix?

  2. #2
    Nessuna idea?
    What is the |\/|atrix?

  3. #3
    up
    What is the |\/|atrix?

  4. #4
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Prova questo (avevi già tutto quel che occereva nel tuo script)

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <SCRIPT LANGUAGE="JavaScript">
    var checkflag = "true";
    function check(field) {
    if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
    field[i].checked = true;}
    checkflag = "true";
    f(document.getElementById('form1'));
    return "Deseleziona Tutti"; }
    else {
    for (i = 0; i < field.length; i++) {
    field[i].checked = false;}
    checkflag = "false";
    f(document.getElementById('form1'));
    return "Seleziona Tutti"; }
    }
    
    function f(frmObj)
    {
     
    var strVal = "";
    var el = frmObj.elements;
    var nFields = el.length;
    var sepStr = "-";
     
    for ( n = 0 ; n < nFields ; n++ )
    {
    if (el[n].type == "checkbox" && el[n].checked)
    {
    if (strVal.length > 0) strVal += sepStr;
    strVal += el[n].value;
    }
    }
    document.form1.who.value = strVal;
    }
    </script>
    
    <form id="form1" name="form1" method="post" action="test.php">
    <input type=button value="Deseleziona Tutti" onClick="this.value=check(this.form.list)"> 
    
    
    <input type=checkbox name=list value="1" checked onClick="f(this.form);">Opzione 1
    
    <input type=checkbox name=list value="2" checked onClick="f(this.form);">Opzione 2
    
    <input type=checkbox name=list value="3" checked onClick="f(this.form);">Opzione 3
    
    <input type=checkbox name=list value="4" checked onClick="f(this.form);">Opzione 4
    
    <input type=checkbox name=list value="5" checked onClick="f(this.form);">Opzione 5
    
    
    <input type=text name=who>
    
    <input type="submit" name="Submit" value="Invia">
    
    </form>
    <script language="JavaScript" type="text/javascript">
    <!--
    f(document.getElementById('form1'));
    //-->
    </script>
    
    </body>
    </html>

  5. #5
    Non so come ringraziarti!
    Ciao!
    What is the |\/|atrix?

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.