Ho scaricato un carrello delle spese in JS da un utente del forum (jacqui) e ho un problema con le cifre.
Per i prezzi se mette sempre cosi, esempio: "15.00" e alla fine quando si visualizza la cifra viene sempre così: "15" o se faccio così: "15.50" diventa "15.5".
Posto il codice, magari potete darmi una mano:
code:
function product(prezzo, prezzo2, euro, name, location, id)
{
this.prezzo = prezzo;
this.prezzo2 = prezzo2;
this.euro = euro;
this.name = name;
this.location = location;
this.id = id;
}
prod01 = new product(17.00, 17.00, 17.00, "- Armadio -", "arm.html#se01", "SE01");
postage = 7.25;
function setCookie(name, value)
{
document.cookie = name + "=" + escape(value);
}
function getCookie(name){
var Search = name + "=";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(Search)
if (offset != -1) {
offset += Search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(offset, end));
}
}
}
function roundToCents(n)
{
cents = n * 100;
cents = Math.round(cents);
strCents = "" + cents;
len = strCents.length;
first = strCents.substring(0, len - 2) + ".";
last = strCents.substring(len - 2, len);
if (first == "."){
first = "0.";
}
if (last.length == 1){
last += "0";
}
return first+last;
}
function checkrequired(which)
{
var pass=true
if (document.images)
{
for (b=0;b<which.length;b++)
{
var tempobj=which.elements[b]
if (tempobj.name.substring(0,8)=="required")
{
if (((tempobj.type=="text"||tempobj.type=="textarea") &&tempobj.value=='')||(tempobj.type.toString().cha rAt(0)=="s"&&tempobj.selectedIndex==-1))
{
pass=false
break
}
}
}
}
if (!pass)
{
alert("Non hai inserito tutti i dati richiesti. Clicca su OK, verifica e completa il form.")
return false
}
else
return true
}
function printLine(pr)
{
product = parent.getCookie(pr.name);
if (product > 0){
prodtotal = product * pr.prezzo2;
quantity = 'quantity' + 3;
document.write("<table border='0' width='500' align='left' bgcolor='#ebebeb'>")
document.write("<tr>")
document.write("<td width='5%' align=left><font face='verdana, arial' size='1'>")
document.write( pr.id);
document.write("<input type='hidden' SIZE='6' NAME=productid", i, " VALUE=", pr.id, ">");
document.write("</td>")
document.write("<td width='12%' align=right><font face='verdana, arial' size='1'>")
document.write( product);
document.write("<input type='hidden' SIZE='6' NAME=quantity", i, " VALUE=", product, ">");
document.write("</td>")
document.write("<td width='13%' align=right><font face='verdana, arial' size='1'>")
document.write(pr.prezzo2);
document.write("<input type='hidden' SIZE='6' NAME=price", i, " VALUE=", 'EUR' + pr.prezzo2, ">");
document.write("</td>")
document.write("<td width='14%' align=right><font face='verdana, arial' size='1'>")
document.write( prodtotal);
document.write("<input type='hidden' SIZE='6' NAME=total", i, " VALUE=",'EUR' + prodtotal, ">");
document.write("</td>")
document.write("<td width='44%' align=center><font face='verdana, arial' size='1'>")
document.write(" ", pr.name, "");
document.write("</td>")
document.write("</font> </tr>")
document.write("</table>")
document.write("
")
calculateTotal(prodtotal);
i++;
}
}
/*-------------------------------------------------------------------------------------------------
questa funzione viene richiamata quando serve nel carrello per ritornare il totale*/
function calculateTotal(num)
{
grandtotal += num;
return grandtotal;
}
function getInitialValue(name)
{
v = parent.getCookie(name);
if (v > 0){
initial = v;
}
else {
initial = 0;
}
return initial;
}
/* e poi c'è un altro file esterno */
<!--
i = 0;
i = 0;
grandtotal = 0;
document.write("<form action=http://FormMail.aruba.it/cgi-bin/FormMail.pl method='post' onsubmit='return Validate(this)' name='FormMail'
printLine(parent.prod01);
document.write("<table border='0' width='500' align='left'>")
document.write("<tr>")
document.write("<td width='13%' align=right><font face='verdana, arial' size='1'>")
document.write("Importo:");
document.write("</font></td>")
document.write("<td width='12%' align=right><font face='verdana, arial' color='green' size='1'>")
document.write(parent.roundToCents(grandtotal));
document.write("</font></td>")
document.write("<input type='hidden' size='6' name='subtotal' value=",'EUR' + parent.roundToCents(grandtotal), ">");
document.write("<td width='13%' align=right><font face='verdana, arial' size='1'>")
document.write("Spese:");
document.write("</font></td>")
document.write("<td width='12%' align=right><font face='verdana' color='green' size='1'>")
document.write( parent.postage);
document.write("</font></td>")
document.write("<input type='hidden' size='6' name='postage' value=",'EUR' + parent.postage, ">");
document.write("<td width='22%' align=right><font face='verdana, arial' size='1'>")
document.write("Totale in Euro:");
document.write("</font></td>")
document.write("<td width='12%' align=right><font face='verdana, arial' color='green' size='1'>")
document.write(parent.roundToCents(grandtotal + parent.postage));
document.write("</font></td>")
document.write("<input type='hidden' size='6' name='grandtotal' value=",'EUR' + parent.roundToCents(grandtotal + parent.postage), ">");
document.write("</tr>")
document.write("</table>
")
Grazie.
________________________________