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

Discussion in 'Javascript' started by Tim, Oct 20, 2003.

  1. Tim

    Tim Guest

    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
    Tim, Oct 20, 2003
    #1
    1. Advertising

  2. Tim

    jon Guest

    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/
    jon, Oct 20, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    3
    Views:
    482
    Roedy Green
    Dec 13, 2005
  2. eslacker

    Need Schema help, pls. TIA!!!

    eslacker, Aug 7, 2003, in forum: XML
    Replies:
    0
    Views:
    351
    eslacker
    Aug 7, 2003
  3. Replies:
    2
    Views:
    1,224
  4. Replies:
    9
    Views:
    523
    CBFalconer
    Apr 25, 2006
  5. Jed
    Replies:
    28
    Views:
    659
    Michael O'Keeffe
    Oct 16, 2005
Loading...

Share This Page