dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Second time around the goldfish bowl

  1. #1
    Join Date
    May 2010
    Posts
    3

    Second time around the goldfish bowl

    I've a JS file that isn't behaving according to my liking. I'm trying to determine the checked and radio buttons associated with a purchase and create hidden fields to pass through to a cart page. The problem is it seems to double-up items in the array. For example, if a user chooses "foursome", there appears two foursomes in the product array.

    The code:

    function CalculateCost(){
    var total = 0;
    var sLevel;
    var holes = Number(document.golfReg2010.holes.value);
    var sassy = document.golfReg2010.elements.length;
    var bugger = document.golfReg2010.sponLevel.length;
    var s = 0;
    for(var i=3;i<6;i++){
    var bub = document.golfReg2010.elements[i].name;
    alert(bub);
    switch(bub){
    case 'individualReg':
    if (document.golfReg2010.elements[i].checked){
    total = total + 150;
    product.push("Individual");
    }
    break;
    case 'foursomeReg':
    if (document.golfReg2010.elements[i].checked){
    total = total + 550;
    product.push("Foursome");
    }
    break;
    case 'sponsorReg':
    if (document.golfReg2010.elements[i].checked){
    while(!sLevel){
    sLevel = GetCheckedValue(document.golfReg2010.sponLevel[s]);
    s++;
    }
    switch(sLevel){
    case "event":
    total = total + 3500;
    product.push("Event Sponsorship");
    break;
    case "premier":
    total = total + 2250;
    product.push("Premier Sponsorship");
    break;
    case "silver":
    total = total + 1200;
    product.push("Silver Sponsorship");
    break;
    case "hole":
    total = total + (350*holes);
    product.push("Hole Sponsorship (" + holes.toString() + " total)");
    break;
    default:
    alert("Please report this as an error to the administrator");
    break;
    }
    }
    break;
    }
    for(var p=0;p<product.length;p++){
    alert(product[p]);
    }
    }
    if(document.golfReg2010.acsContribution.value){
    total = total + Number(document.golfReg2010.acsContribution.value);
    product.push("ACS Contribution");
    }
    total = total + ".00"
    document.getElementById("UMamount").value = total;
    }

  2. #2
    Join Date
    May 2010
    Posts
    3

    Angry So it's something in the switch

    So basically I'm not understanding why:

    The switch statement seems to be worthless for looping through a section of the form and determining which items are checked. On every trip around, it's still getting into the previous CASE item and adding to the array. So if I choose the individual checkbox (first in the order), I'll end up with it appearing three times in the array of product. Seems to me the CASE statement shouldn't evaluate the item each time, since the variable changes on each loop through. What is going on here that is does this?

  3. #3
    Join Date
    May 2010
    Posts
    213
    Could you upload the whole form with the script on a server and post the link? If it's not too much trouble, remove all other crap from the whole page but leave the form fully functional. And we'll go from there. If you can't do that, zip it all up and post here.

  4. #4
    Join Date
    May 2010
    Posts
    3

    Ok, sorry for the delay

    I've been working on other projects, so I apologize for the delay in posting. Attached is an HTML file with both the form and associated Javascript code. Normally, the JS is in a separate file FYI.

    As I stated, It would seem to me that the Case Switch wouldn't add multiple instances of a product to the array Product since each time around the For loop, the variable will change to the next form item.
    Attached Files Attached Files
    Last edited by Techmaniac; 05-14-2010 at 10:21 AM. Reason: attachment didn't work

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles