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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

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

    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 TemporaryShippi ng = 0;

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

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

    if ((document.Form .Item3.selected Index == 1) ||
    (document.Form. Item3.selectedI ndex == 2) ||
    (document.Form. Item3.selectedI ndex == 3))
    {
    NewQuantity3="T rue";
    }
    else
    {
    NewQuantity3="F alse";
    }

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

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

    Tim


  • jon

    #2
    Re: Tricky Addition Question In Javascript...Pl ease help. TIA

    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,whi ch) {
    lastSelected = lastItemArray[which]
    if ((what.selected Index>0 && what.selectedIn dex<4) &&
    !(lastSelected> 0 && lastSelected<4) ) {
    ShippingTotal+= 7;
    }
    else if ((what.selected Index<1 || what.selectedIn dex>3) &&
    (lastSelected<1 || lastSelected>3) ) {
    ShippingTotal-=7;
    }
    if (ShippingTotal< 0) {
    ShippingTotal=0 ;
    }
    document.Form.S hipping.value = ShippingTotal;
    lastItemArray[which]=what.selectedI ndex;
    }


    Arrays are useful. Hope that helps.

    jon


    Comment

    Working...