turtz_01
12-04-2003, 06:29 PM
What i would like to know, if it is possible, is can i get a $ sign to display infront of a number when displaying the value in the textfield of a form?
I have made up an order form where a user can purchase wine by selecting the quantity, by the bottle or by the dozen, from two dropdown boxes in each row.
My script adds the values of these two boxes (which are the prices) and displays the answer in a sub-total column (ie sub1, sub2, sub3, etc. It then calculates how many cartons are needed to package the bottles based on 12 to a carton, works out the delivery charge and combines all the costs to give a total.
All the variables that need a $ use the custRound function (rounds to two decimal points). Maybe it can be included in this somehow? Can someone show me how?
This is my first attempt at javascript and I have only been at it for 8 hours so please don't laugh if my code isn't efficient.
If you have any suggestions on how i can simplify it or do something more efficiently, i'd be eager to know.
Here is the code:
<!-- Begin
function doMath() {
var one = eval(document.order.quantity_b1.value)
var two = eval(document.order.quantity_d1.value)
var three = eval(document.order.quantity_b2.value)
var four = eval(document.order.quantity_d2.value)
var five = eval(document.order.quantity_b3.value)
var six = eval(document.order.quantity_d3.value)
var seven = eval(document.order.quantity_b4.value)
var eight = eval(document.order.quantity_d4.value)
var nine = eval(document.order.quantity_b5.value)
var ten = eval(document.order.quantity_d5.value)
var eleven = eval(document.order.quantity_b6.value)
var twelve = eval(document.order.quantity_d6.value)
var thirteen = eval(document.order.quantity_b7.value)
var fourteen = eval(document.order.quantity_d7.value)
var fifteen = eval(document.order.quantity_b8.value)
var sixteen = eval(document.order.quantity_d8.value)
//ads up the total cost of bottles and dozens for a particular wine
var sub1 = one + two
var sub2 = three + four
var sub3 = five + six
var sub4 = seven + eight
var sub5 = nine + ten
var sub6 = eleven + twelve
var sub7 = thirteen + fourteen
var sub8 = fifteen + sixteen
//counts the total numer of bottles in the order and then returns how many cartons are needed to package them
var bottles = (sub1 / 28) + (sub2 / 28.5) + (sub3 / 29) + (sub4 / 28) + (sub5 / 28) + (sub6 / 28.5) + (sub7 / 30) + (sub8 / 30)
var cartons = Math.ceil(bottles / 12)
//calculates the delivery cost
var delivercost = cartons * 10
//ads up the total cost of the order
var total = sub1 + sub2 + sub3 + sub4 + sub5 + sub6 + sub7 + sub8 + delivercost
document.order.subtotal_1.value=custRound(sub1);
document.order.subtotal_2.value=custRound(sub2);
document.order.subtotal_3.value=custRound(sub3);
document.order.subtotal_4.value=custRound(sub4);
document.order.subtotal_5.value=custRound(sub5);
document.order.subtotal_6.value=custRound(sub6);
document.order.subtotal_7.value=custRound(sub7);
document.order.subtotal_8.value=custRound(sub8);
document.order.cartons.value=(cartons);
document.order.delivercost.value=custRound(delivercost);
document.order.total.value=custRound(total);
}
//returns values with two decimal places.
function custRound(n) {
var s = "" + Math.round(n * 100) / 100
var i = s.indexOf('.')
if (i < 0) return s + ".00"
var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
if (i + 2 == s.length) t += "0"
return t
}
// End -->
I have made up an order form where a user can purchase wine by selecting the quantity, by the bottle or by the dozen, from two dropdown boxes in each row.
My script adds the values of these two boxes (which are the prices) and displays the answer in a sub-total column (ie sub1, sub2, sub3, etc. It then calculates how many cartons are needed to package the bottles based on 12 to a carton, works out the delivery charge and combines all the costs to give a total.
All the variables that need a $ use the custRound function (rounds to two decimal points). Maybe it can be included in this somehow? Can someone show me how?
This is my first attempt at javascript and I have only been at it for 8 hours so please don't laugh if my code isn't efficient.
If you have any suggestions on how i can simplify it or do something more efficiently, i'd be eager to know.
Here is the code:
<!-- Begin
function doMath() {
var one = eval(document.order.quantity_b1.value)
var two = eval(document.order.quantity_d1.value)
var three = eval(document.order.quantity_b2.value)
var four = eval(document.order.quantity_d2.value)
var five = eval(document.order.quantity_b3.value)
var six = eval(document.order.quantity_d3.value)
var seven = eval(document.order.quantity_b4.value)
var eight = eval(document.order.quantity_d4.value)
var nine = eval(document.order.quantity_b5.value)
var ten = eval(document.order.quantity_d5.value)
var eleven = eval(document.order.quantity_b6.value)
var twelve = eval(document.order.quantity_d6.value)
var thirteen = eval(document.order.quantity_b7.value)
var fourteen = eval(document.order.quantity_d7.value)
var fifteen = eval(document.order.quantity_b8.value)
var sixteen = eval(document.order.quantity_d8.value)
//ads up the total cost of bottles and dozens for a particular wine
var sub1 = one + two
var sub2 = three + four
var sub3 = five + six
var sub4 = seven + eight
var sub5 = nine + ten
var sub6 = eleven + twelve
var sub7 = thirteen + fourteen
var sub8 = fifteen + sixteen
//counts the total numer of bottles in the order and then returns how many cartons are needed to package them
var bottles = (sub1 / 28) + (sub2 / 28.5) + (sub3 / 29) + (sub4 / 28) + (sub5 / 28) + (sub6 / 28.5) + (sub7 / 30) + (sub8 / 30)
var cartons = Math.ceil(bottles / 12)
//calculates the delivery cost
var delivercost = cartons * 10
//ads up the total cost of the order
var total = sub1 + sub2 + sub3 + sub4 + sub5 + sub6 + sub7 + sub8 + delivercost
document.order.subtotal_1.value=custRound(sub1);
document.order.subtotal_2.value=custRound(sub2);
document.order.subtotal_3.value=custRound(sub3);
document.order.subtotal_4.value=custRound(sub4);
document.order.subtotal_5.value=custRound(sub5);
document.order.subtotal_6.value=custRound(sub6);
document.order.subtotal_7.value=custRound(sub7);
document.order.subtotal_8.value=custRound(sub8);
document.order.cartons.value=(cartons);
document.order.delivercost.value=custRound(delivercost);
document.order.total.value=custRound(total);
}
//returns values with two decimal places.
function custRound(n) {
var s = "" + Math.round(n * 100) / 100
var i = s.indexOf('.')
if (i < 0) return s + ".00"
var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
if (i + 2 == s.length) t += "0"
return t
}
// End -->