ho vari campi che dovrebbero contenere valori numerici con due decimali.
se scrivo 123 all'uscita dal campo dovrebbe scrivere 123,00-
come posso utilizzare la funzione toFixed()??
ho vari campi che dovrebbero contenere valori numerici con due decimali.
se scrivo 123 all'uscita dal campo dovrebbe scrivere 123,00-
come posso utilizzare la funzione toFixed()??
in questo caso;
ho questo input:
<input type="text" name="importo" onblur=".....?????>
come posso utilizzare laf funzione??
boh... io farei così...
codice:<!doctype html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Language" content="it" /> <title>Esempio per il Forum di HTML.it</title> <script type="text/javascript"> function fissaValore() { document.miaForm.importo.setUserData("sOldValue", parseFloat(document.miaForm.importo.value).toFixed(2), null); } function controllaValore() { if (isFinite(document.miaForm.importo.value)) { fissaValore(); } document.miaForm.importo.value = document.miaForm.importo.getUserData("sOldValue"); } </script> </head> <body onload="fissaValore();"> <form name="miaForm"> <input type="text" name="importo" onblur="controllaValore();" value="0.00" /> </form> </body> </html>
anche se io ho 7 campi da controllare?
codice:<!doctype html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Language" content="it" /> <title>Esempio per il Forum di HTML.it</title> <script type="text/javascript"> function storeValue(oToBeFixed) { oToBeFixed.setUserData("oldValue", parseFloat(oToBeFixed.value).toFixed(2), null); } function checkValue(oMyField) { if (isFinite(oMyField.value)) { storeValue(oMyField); } oMyField.value = oMyField.getUserData("oldValue"); } function initFixedFields() { storeValue(document.miaForm.importo1); storeValue(document.miaForm.importo2); storeValue(document.miaForm.importo3); storeValue(document.miaForm.importo4); storeValue(document.miaForm.importo5); storeValue(document.miaForm.importo6); storeValue(document.miaForm.importo7); } </script> </head> <body onload="initFixedFields();"> <form name="miaForm"> <input type="text" name="importo1" onchange="checkValue(this);" value="0.00" /> <input type="text" name="importo2" onchange="checkValue(this);" value="0.00" /> <input type="text" name="importo3" onchange="checkValue(this);" value="0.00" /> <input type="text" name="importo4" onchange="checkValue(this);" value="0.00" /> <input type="text" name="importo5" onchange="checkValue(this);" value="0.00" /> <input type="text" name="importo6" onchange="checkValue(this);" value="0.00" /> <input type="text" name="importo7" onchange="checkValue(this);" value="0.00" /> </form> </body> </html>
ho provato cosi:
<script type="text/javascript">
function storeValue(oToBeFixed) {
oToBeFixed.setUserData("oldValue", parseFloat(oToBeFixed.value).toFixed(2), null);
}
function checkValue(oMyField) {
if (isFinite(oMyField.value)) { storeValue(oMyField); }
oMyField.value = oMyField.getUserData("oldValue");
}
function initFixedFields() {
storeValue(document.form1.importo1);
storeValue(document.form1.importo2);
}
</script>
<body onload="initFixedFields();">
nei campi testo= onChange="checkValue(this);"
ma non funziona
non è che devo correnggere
function storeValue
Per come avevo scritto la funzione i campi devono avere un valore predefinito e ben formattato all'inizio (io avevo messo value="0.00"). Se vuoi evitare tutto ciò, puoi fare così:
Se i tuoi campi hanno davvero come nome «importo + [NUMERO DA 1 A 7]», puoi semplificare la funzione initFixedFields così:codice:<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Language" content="it" /> <title>Esempio per il Forum di HTML.it</title> <script type="text/javascript"> function checkValue(oToBeFixed) { var nParsedNum = parseFloat(oToBeFixed.value); if (isFinite(nParsedNum)) { oToBeFixed.setUserData("fixedValue", nParsedNum.toFixed(2), null); } oToBeFixed.value = oToBeFixed.getUserData("fixedValue"); } function initFixedFields() { document.form1.importo1.setUserData("fixedValue", "0.00", null); document.form1.importo2.setUserData("fixedValue", "0.00", null); document.form1.importo3.setUserData("fixedValue", "0.00", null); document.form1.importo4.setUserData("fixedValue", "0.00", null); document.form1.importo5.setUserData("fixedValue", "0.00", null); document.form1.importo6.setUserData("fixedValue", "0.00", null); document.form1.importo7.setUserData("fixedValue", "0.00", null); } </script> </head> <body onload="initFixedFields();"> <form name="form1"> <input type="text" name="importo1" onblur="checkValue(this);" /> <input type="text" name="importo2" onblur="checkValue(this);" /> <input type="text" name="importo3" onblur="checkValue(this);" /> <input type="text" name="importo4" onblur="checkValue(this);" /> <input type="text" name="importo5" onblur="checkValue(this);" /> <input type="text" name="importo6" onblur="checkValue(this);" /> <input type="text" name="importo7" onblur="checkValue(this);" /> </p> </form> </body> </html>
Ah, dimenticavo! Ho usato degli standard piuttosto nuovi:codice:function initFixedFields() { for (var nFieldId = 1; nFieldId < 8; nFieldId++) { document.form1["importo" + nFieldId].setUserData("fixedValue", "0.00", null); } }
- http://www.w3.org/TR/DOM-Level-3-Cor...e3-getUserData
- http://www.w3.org/TR/DOM-Level-3-Cor...e3-setUserData
...magari controlla che funzioni su tutti i maggiori browser!!
P.S. Ho anche notato che funziona meglio se sostituiamo l'evento onchange con onblur, come avevi scritto tu all'inizio...![]()
come nome campi sono:
lordo
tara
netto
Ma non erano sette??Originariamente inviato da tigre2209
come nome campi sono:
lordo
tara
netto![]()