Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Contatore checkbox spuntati

    Salve a tutti,
    ho trovato uno script che mi conta tutti i checkbox spuntati, ma dovrei fare una piccola modifica che non sono in grado di fare non conoscendo praticamente del tutto lo javascript. Lo script e' il seguente:
    <SCRIPT LANGUAGE="JavaScript">
    function anyCheck(form) {
    var total = 0;
    var max = form.ckbox.length;
    for (var idx = 0; idx < max; idx++) {
    if (eval("document.playlist.ckbox[" + idx + "].checked") == true) {
    total += 1;
    }
    }
    alert("You selected " + total + " boxes.");
    }
    // End -->
    </script>

    </HEAD>



    <BODY>

    <form method="post" name=playlist action=''>

    2<input type=checkbox name="ckbox[]" value=2 onClick="anyCheck(this.form)">

    3<input type=checkbox name=ckbox[] value=3 onClick="anyCheck(this.form)">

    4<input type=checkbox name=ckbox[] value=4 onClick="anyCheck(this.form)">


    <input type=submit value="Count Checkboxes">
    </form>



    <center>

    Lo script funziona perfettamente se invece di <input type=checkbox name="ckbox[]" value=2 onClick="anyCheck(this.form)"> mettessi <input type=checkbox name="ckbox" value=2 onClick="anyCheck(this.form)">. Ma a me servono necessariamente le due parantesi quadre, infatti mi serve che le checkbox vengano riconosciute come un array per far girare il successivo script in php. Penso che i problemi siano in queste due righe:
    var max = form.ckbox.length;
    if (eval("document.playlist.ckbox[" + idx + "].checked") == true) {
    ho provato a sostituire var max = form.ckbox.length; con var max = form.ckbox[].length; ma il sistema mi riconosce un errore. Inoltre se fosse possibile, non vorrei un alert a segnalarmi il numero di checkbox spuntati, ma vorrei un semplice iinput=text se fosse possibile.
    Ringrazio l'anima/e pia che mi vorra'/vorranno aiutare.

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Era scritto malissimo&hellip; Cmq ci avevi azzeccato

    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Pagina vuota</title>
    <script type="text/javascript">
    function anyCheck(oForm) {
    	for (var iChBx = 0, nCount = 0, nChBxs = oForm["ckbox[]"].length; iChBx < nChBxs; iChBx++) {
    		if (oForm["ckbox[]"][iChBx].checked) { nCount += 1; }
    	}
    	alert("You selected " + nCount + " boxes.");
    }
    </script>
    </head>
    
    <body>
    <form method="post" name="playlist" action="">
    <fieldset>
    <legend>Conta checkbox</legend>
    
    
    2<input type="checkbox" name="ckbox[]" value="2" onclick="anyCheck(this.form);">
    
    3<input type="checkbox" name="ckbox[]" value="3" onclick="anyCheck(this.form);">
    
    4<input type="checkbox" name="ckbox[]" value="4" onclick="anyCheck(this.form);"></p>
    
    
    <input type="submit" value="Count Checkboxes"></p>
    </fieldset>
    </form>
    </body>
    </html>

  3. #3
    Ti ringrazio per la riscrittura dello js. Invece di un allert si puo' definire una var e farla leggere successivamente nella value di un input text??

    Grazie comunque tanto

  4. #4
    Simile a questo
    <script>
    function calc(val){

    var totalval = document.getElementById('total').innerHTML * 1;
    document.getElementById('total').innerHTML = (totalval + parseInt(val));

    }
    </script>


    <input value="554.98" type="checkbox" onclick="calc(this.value)">
    <input value="234.98" type="checkbox" onclick="calc(this.value)">
    <input value="5534.98" type="checkbox" onclick="calc(this.value)">



    <p id="total"></p>
    Infatti il totale me lo restituisce come testo in fondo alla pagina. Grazie

  5. #5
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Pagina vuota</title>
    <script type="text/javascript">
    function anyCheck(oField) {
    	for (var iChBx = 0, nCount = 0, sName = oField.name, oForm = oField.form, nChBxs = oForm[sName].length; iChBx < nChBxs; iChBx++) {
    		if (oForm[sName][iChBx].checked) { nCount++; }
    	}
    	return nCount;
    }
    </script>
    </head>
    
    <body onload="document.playlist.total.value=anyCheck(document.playlist['ckbox[]'][0]);">
    <form method="post" name="playlist" action="#">
    <fieldset>
    <legend>Conta checkbox</legend>
    
    
    <input type="checkbox" name="ckbox[]" value="tizio" onclick="this.form.total.value=anyCheck(this);" id="tuoId1" /> <label for="tuoId1">primo valore</label>
    
    <input type="checkbox" name="ckbox[]" value="caio" onclick="this.form.total.value=anyCheck(this);" id="tuoId2" /> <label for="tuoId2">secondo valore</label>
    
    <input type="checkbox" name="ckbox[]" value="sempronio" onclick="this.form.total.value=anyCheck(this);" id="tuoId3" /> <label for="tuoId3">etc. etc.</label>
    
    Numero di checkbox selezionate: <input type="text" name="total" size="2" readonly /></p>
    
    
    <input type="submit" value="Count Checkboxes"></p>
    </fieldset>
    </form>
    </body>
    </html>

  6. #6
    Grazie sei stato veramente utile e preciso. Ti ringrazio enormemente

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.