Questa soluzione del + e della variabile fuori funzione l'avevo provata anch'io ma l'ho abbandonata perché incrementa a prescindere anche se cali/modifichi le quantità/prodotti, l'alternativa trovata e questa
codice:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language=Javascript> function calcola() { var prodotto1 = document.getElementById('tipo') var tipo1 = parseFloat(prodotto1.options[prodotto1.selectedIndex].value.split("|")[1]) var qt1 = document.getElementById('quantita') var somma1 = (parseInt(qt1.options[qt1.selectedIndex].value) * tipo1) var prodotto2 = document.getElementById('tipo1') var tipo2 = prodotto2.options.selectedIndex!=0 ? parseFloat(prodotto2.options[prodotto2.selectedIndex].value.split("|")[1]) : 0 var qt2 = document.getElementById('quantita1') var somma2 = qt2.options.selectedIndex!=0 ? (parseInt(qt2.options[qt2.selectedIndex].value) * tipo2) : (0 * tipo2) var valuta = "€ "; document.getElementById('prezzo').innerHTML = valuta+(somma2 + somma1).toFixed(2); document.getElementById('price').value = (somma2 + somma1); } </script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form> Pasta <select name="tipo" id="tipo" onChange="if(this.options.selectedIndex!=0 && document.getElementById('quantita').options.selectedIndex!=0){calcola()}"> <option value="seleziona">Scegli il tipo</option> <option value="Spaghetti|1.2">Spaghetti (€1,20/Kg)</option> <option value="Linguine|1.3">Linguine (€1,30/Kg)</option> <option value="Riso|1.6">Riso (€1,60/Kg)</option> </select> <select name="quantita" id="quantita" onChange="if(this.options.selectedIndex!=0 && document.getElementById('tipo').options.selectedIndex!=0 ){calcola()}else{alert('seleziona una quantita')}"> <option value="seleziona">Quantità</option> <option value="1">1 Kg</option> <option value="2">2 Kg</option> <option value="3">3 Kg</option> <option value="4">4 Kg</option> <option value="5">5 Kg</option> </select> Aqua <select name="tipo2" id="tipo1" onChange="if(this.options.selectedIndex!=0 && document.getElementById('quantita1').options.selectedIndex!=0){calcola()}"> <option value="seleziona">Scegli il tipo</option> <option value="Lete (€ 1,20/L)|1.2">Lete (€ 1,20/L)</option> <option value="Ferrarelle (€ 1,30/L)|1.3">Ferrarelle (€ 1,30/L) </option> <option value="Uliveto (€1,60/L)|1.6">Uliveto (€ 1,60/L)</option> </select> </label> <label> <select name="quantita" id="quantita1" onChange="if(this.options.selectedIndex!=0 && document.getElementById('tipo1').options.selectedIndex!=0 ){calcola()}else{alert('seleziona una quantita')}"> <option value="seleziona">Quantità</option> <option value="1">1 L</option> <option value="2">2 L</option> <option value="3">3 L</option> <option value="4">4 L</option> <option value="5">5 L</option> </select> <input type="hidden" name="Totale spesa" id="price"> </label> <div align="left" id="prezzo">€ 0</div> </form> </body> </html>

Rispondi quotando