Salve ragazzi,ho un dubbio.Volevo sapere se magari fosse possibile riconoscere se un elemento input text avesse o meno il focus.Non so se mi sono spiegato bene,ma spero di si.Praticamente ho una funzione che mi deve calcolare il totale e il totale parziale in un carrello all' onChange del input di testo Quantità . Vi posto il codice HTML :
ed le funzioni javascript :codice:<?php if(!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="Il mio sito"'); header('HTTP/1.0 401 Unauthorized'); echo 'Autenticazione fallita'; exit; } else { echo "<center><h1><font color=red>Benvenuto {$_SERVER['PHP_AUTH_USER']} </font></h1></center>"; } ?> <html> <head> <script type="text/javascript" src="funz2javascript.js"> </script> </head> <body> <center> <table border=5> <tr><th align=center colspan=2>OPZIONI</th></tr> <tr><td width=35%> <table rules=none frame=void><tr><th colspan=2 align=center>ACCESSORI</th></tr> <form name="accessori"> <?php $db = mysql_connect(localhost,root,federica) or die("Impossibile connettersi"); mysql_select_db("NEGOZIO",$db); $query= "SELECT * FROM ACCESSORI"; $result1 = mysql_query($query,$db); $i=0; while($records1 = mysql_fetch_row($result1)){ echo "<tr><td align=left>$records1[1]</td><td align=right><input type='checkbox' name='check{$i}' value=$records1[2] onclick='aggiungiAccessori();'></input></td><td align=left>$records1[2]</td></tr>"; $i++; } ?> </form> </table> </td> <td width=65% align="center" rowspan=2> <table rules=none frame=void> <form name="vini"> <tr><th align=center colspan=4>VINI</th></tr> <tr><th align=center>VINO</th><th align=center>PREZZO</th><th align=center>Q.TA'</th><th align=center>TOT.PARZ.</th></tr> <?php $query= "SELECT * FROM VINI"; $result = mysql_query($query,$db); $i=0; while($records = mysql_fetch_row($result)){ echo "<tr><td align=left>$records[1]</td><td align=left><span>$records[2]</span></td><td align=center><input type='text' size=2 onChange='calcolaVini();'></td><td align=center><input type='text' name='T{$i}' size=5></td></tr>"; $i++; } ?> </form> <form name="risultato"> <tr><th colspan=3 align=right>IVA 4%</th><td align=center><input type="text" name="iva" size=5 value="0,00" readonly></td></tr> <tr><th colspan=3 align=right>TOTALE</th><td align=center><input type="text" name="totale" id="totale" size=5 value="0,00" readonly></input></td></tr> </form> </table> </td></tr> <tr><td> <table rules=none frame=void><tr><th colspan=2 align=center>SPEDIZIONI</th></tr> <form name="spedizioni"> <tr><td align=left>Posta celere 3</td><td align=left><input type="radio" name="radio" onClick="aggiungiSpedizione();" value="6,00">6.00</td></tr> <tr><td align=left>Posta celere 1</td><td align=left><input type="radio" name="radio" onClick="aggiungiSpedizione();" value="9,00">9.00</td></tr> <tr><td align=left>Assicurata</td><td align=left><input type="radio" name="radio" onClick="aggiungiSpedizione();" value="5,00">5.00</td></tr> </form> </table> </td> </tr> </table> <form name=invio> <center><input type="reset" name="reset" value="reset" onClick="document.risultato.totale.value='0,00';"></input> <input type="submit" name=creaPreventivo value="creaPreventivo"></input></center> </center> </form> </body> </html>
[CODE]Il problema è che quando calcola il totale aggiunge sempre un totale parziale in più,dato che lo vede settato in precedenza.Ragazzi lo so che è un pò incasinata la cosa,ma vi prego datemi una mano perchè sto impazzendo.Grazie.codice:function aggiungiSpedizione(){ var count = document.spedizioni.length; for(var i=0; i<count; i++){ if(document.spedizioni.elements[i].checked == true){ var spedizione = document.spedizioni.elements[i].value; spedizione = parseFloat(spedizione); document.risultato.totale.value = spedizione; } } return spedizione; } function aggiungiAccessori(){ var count = document.accessori.length; tot = 0; for(var j=0; j<count; j++){ if(document.accessori.elements[j].checked == true){ var x = document.accessori.elements[j].value; x = parseFloat(x); tot = parseFloat(tot); tot = tot + x; } document.risultato.totale.value = tot; } return tot; } function aggiungiVini(i,j){ var count = document.vini.length; var theSpan = document.getElementsByTagName("span"); var k = document.vini.elements[j].value; var x = theSpan[i].innerHTML; var totParz = x * k; document.vini.elements[j+1].value = totParz; return totParz; } function calcolaVini(){ if(document.vini.elements[0].value){ var tp = aggiungiVini(0,0); var tot = document.risultato.totale.value; tot = parseFloat(tot); tp = parseFloat(tp); tot = tot + tp; document.risultato.totale.value = tot; } if(document.vini.elements[2].value){ var tp = aggiungiVini(1,2); var tot = document.risultato.totale.value; tot = parseFloat(tot); tp = parseFloat(tp); tot = tot + tp; document.risultato.totale.value = tot; } if(document.vini.elements[4].value){ var tp = aggiungiVini(2,4); var tot = document.risultato.totale.value; tot = parseFloat(tot); tp = parseFloat(tp); tot = tot + tp; document.risultato.totale.value = tot; }
Ciao

Rispondi quotando
, io ti suggerisco di scriverlo una sola volta alla fine del calcolo del totale sia per rintracciare + facilmente eventuali errori di calcolo e sia per motivi di performance (pensa se avessi un botto di oggetti..
)