Tricky Addition Question In Javascript...Please help. TIA

T

Tim

Hello,

I'm extremely puzzled; I cannot figure out what I'm doing wrong. Here's
the situation. I would really appreciate any suggestions.

I'm modifying a shopping cart in the following way. I've just added new
prices to several drop-downs on our javascript-based ordering page. These
new prices must have shipping costs added to them at the bottom of the
ordering system in a text box called "Shipping", while the existing prices
do not have to have shipping costs added.

Basically this means that if the user selects the first 3 options in the
drop-down boxes, the text box of "Shipping" must show "7.00", but if any
option after that is selected, the text box of "Shipping" must show "0.00".
Oh, and here's another wrinkle: only certain drop-down boxes will need this
modification. Also, if a customer selects more than one of these "7.00"
items, an additional "7.00" must be added to their shipping costs.

Any suggestions on how to best implement this? I've tried the following
code, but it isn't working--specifically, I can't the shipping to be
properly affected if a users should move from a "7.00 Shipping" option to a
"0.00" option..

var ShippingTotal = 0;
var ShippingAdded = 7;
var TemporaryShipping = 0;

var NewQuantity1 = "False";
var NewQuantity3 = "False";

....[this is the same code for all drop-down boxes]

if ((document.Form.Item3.selectedIndex == 1) ||
(document.Form.Item3.selectedIndex == 2) ||
(document.Form.Item3.selectedIndex == 3))
{
NewQuantity3="True";
}
else
{
NewQuantity3="False";
}

if (NewQuantity3=="True")
{
ShippingTotal += ShippingAdded;
document.Form.Shipping.value = ShippingTotal;
}
else if (NewQuantity3=="False")
{
document.Form.Shipping.value = ShippingTotal;
}

Again, any help would be greatly appreciated. Thanks in advance.

Tim
 
J

jon

It's a darn tricky problem. Good workout. I did this (which assumes
you are using onChange events and that there is a "none" option at
index 0 for all boxes)

var ShippingTotal = 0;

//arrayItem for every selectbox to store last index selected

var lastItemArray = new Array();
lastItemArray[0] = 0;
lastItemArray[1] = 0;
lastItemArray[2] = 0;

// onChange event function for all select boxes (only one function)
// what is form element object
// which is id num manually passed in for array

function reCalc(what,which) {
lastSelected = lastItemArray[which]
if ((what.selectedIndex>0 && what.selectedIndex<4) &&
!(lastSelected>0 && lastSelected<4)) {
ShippingTotal+=7;
}
else if ((what.selectedIndex<1 || what.selectedIndex>3) &&
(lastSelected<1 || lastSelected>3)) {
ShippingTotal-=7;
}
if (ShippingTotal<0) {
ShippingTotal=0;
}
document.Form.Shipping.value = ShippingTotal;
lastItemArray[which]=what.selectedIndex;
}


Arrays are useful. Hope that helps.

jon

http://www.gurupika.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top