Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di provasp
    Registrato dal
    Sep 2002
    Messaggi
    160

    Somma di campi text in tempo reale

    Ho una pagina contentente parecchi campi text in cui andranno dati di tipo numerico.
    Come faccio a realizzare un campo text che mostra in tempo reale la somma di tutti gli altri campi?

    GRAZIE!

  2. #2
    Utente di HTML.it L'avatar di willybit
    Registrato dal
    May 2001
    Messaggi
    4,367
    Ciao provasp,

    come si chiamano i campi di testo?
    posta un esempio di come vorresti il form

  3. #3
    Utente di HTML.it L'avatar di provasp
    Registrato dal
    Sep 2002
    Messaggi
    160
    Ora mi spiego correttamente:

    Io ho 30 campi di testo;
    10 di questi si chiamano a1, a2, a3, a4...
    altri 10 si chiamano b1, b2, b3, b4...
    e gli ultimi 10 c1, c2, c3...

    Al di sotto di questi campi, ne ho altri 3 che si chiamano a_tot, b_tot, c_tot .
    Io vorrei che in a_tot mi facesse vedere la somma: a1 + a2 + a3...
    in b_tot la somma b1 + b2 + b3...
    e in c_tot la somma c1 + c2 + c3...


    Spero si capisca...

  4. #4
    Utente di HTML.it L'avatar di willybit
    Registrato dal
    May 2001
    Messaggi
    4,367
    ecco qua
    codice:
    <HTML>
    <HEAD>
    <script language="javascript">
    function SoloNumeri(obj){
    	var segno=''
    	var RE = /^-/
    	if(RE.test(obj.value)){
    		segno='-'
    		obj.value=obj.value.replace(RE,'')
    	}
    	RE = /\d*[,.]?\d*/
    	if(obj.value.match(RE)==null)
    		obj.value=''
    	else{
    		var numero = obj.value.match(RE)[0]
    		obj.value=segno+numero
    	}
    }
    function Somma(tt){
    	var ff = tt.form
    	SoloNumeri(tt);
    	var pre = tt.name.substr(0,1)
    	var somma = 0
    	for(i=0;i<10;i++)
    		if(ff.elements[pre+(i+1)].value!='')
    			somma+=parseInt(ff.elements[pre+(i+1)].value,10)
    	ff.elements[pre+'_tot'].value=somma
    }
    </script>
    </HEAD>
    <BODY>
    <form name="NomeForm">
    <table>
      <tr>
        <td><input type="text" name="a1" onchange="Somma(this);"></td>
        <td><input type="text" name="b1" onchange="Somma(this);"></td>
        <td><input type="text" name="c1" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a2" onchange="Somma(this);"></td>
        <td><input type="text" name="b2" onchange="Somma(this);"></td>
        <td><input type="text" name="c2" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a3" onchange="Somma(this);"></td>
        <td><input type="text" name="b3" onchange="Somma(this);"></td>
        <td><input type="text" name="c3" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a4" onchange="Somma(this);"></td>
        <td><input type="text" name="b4" onchange="Somma(this);"></td>
        <td><input type="text" name="c4" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a5" onchange="Somma(this);"></td>
        <td><input type="text" name="b5" onchange="Somma(this);"></td>
        <td><input type="text" name="c5" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a6" onchange="Somma(this);"></td>
        <td><input type="text" name="b6" onchange="Somma(this);"></td>
        <td><input type="text" name="c6" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a7" onchange="Somma(this);"></td>
        <td><input type="text" name="b7" onchange="Somma(this);"></td>
        <td><input type="text" name="c7" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a8" onchange="Somma(this);"></td>
        <td><input type="text" name="b8" onchange="Somma(this);"></td>
        <td><input type="text" name="c8" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a9" onchange="Somma(this);"></td>
        <td><input type="text" name="b9" onchange="Somma(this);"></td>
        <td><input type="text" name="c9" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td><input type="text" name="a10" onchange="Somma(this);"></td>
        <td><input type="text" name="b10" onchange="Somma(this);"></td>
        <td><input type="text" name="c10" onchange="Somma(this);"></td>
      </tr>
      <tr>
        <td colspan="3"><hr></td>
      </tr>
      <tr>
        <td><input type="text" name="a_tot" readonly></td>
        <td><input type="text" name="b_tot" readonly></td>
        <td><input type="text" name="c_tot" readonly></td>		
      </tr>
    </table>
    </form>
    </BODY>
    </HTML>

  5. #5
    Utente di HTML.it L'avatar di provasp
    Registrato dal
    Sep 2002
    Messaggi
    160
    WOW che velocità!

    stasse lo provo e poi ti faccio sapere...

    GRAZIE 1000...

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.