简体   繁体   中英

Javascript arrays with dynamic forms

this one is driving me nuts so I certainly hope the SO community can shed some light on this for me. The problem is that I am creating an order form where the user has two options to select from (reports or ratings) which dynamically populate a quantity drop down. In order to calculate the total of their orders I believe I have to turn the drop downs into arrays, however I have no idea how to do this for the dynamic drop down. To clarify the array is supposed to associate a monetary value to every option. Here is some code:

Javascript for dynamic options:

     function dropdownlist(listindex)
      {
       document.form.rnr.options.length = 0;

      switch (listindex)
     {

     case "0" :
          document.form.rnr.options[0]=new Option("1 - $40","1");
          document.form.rnr.options[1]=new Option("10 - $350 ($35 ea)","10");
          document.form.rnr.options[2]=new Option("25 - $700 ($28 ea)","25");
          document.form.rnr.options[3]=new Option("50 - $1200 ($24 ea)","50");
          document.form.rnr.options[4]=new Option("100 - $2000 ($20 ea)","100");
     break;

     case "1" :
          document.form.rnr.options[0]=new Option("Up to 249 - $750 (~$3 ea.)","249");
          document.form.rnr.options[1]=new Option("Up to 499  - $1375 (~$2.75 ea.)","499");
          document.form.rnr.options[2]=new Option("Up to 1000 - $2500 (~$2.50 ea)","1000");
          document.form.rnr.options[3]=new Option("1000+ - $4000","100000");

     break;

        }
     return true;
     }

And the example code that I found online of what I am trying to do as far as arrays is this:

     var cake_prices = new Array();
     cake_prices["Round6"]=20;
     cake_prices["Round8"]=25;
     cake_prices["Round10"]=35;
     cake_prices["Round12"]=75;

Please ignore the fact the example uses cakes, a cake order form was the example used by the developer for the tutorial I am using, unfortunately the tutorial is for static data. Thanks in advance, and if any clarification is needed or any additional code is required feel free to let me know!

ADDITION:

Dropdown is called in the form like this:

     <select class="form" name="type" id="type" style="height:26px;" onchange="javascript:dropdownlist(this.options[this.selectedIndex].value);"> 
     <option value="" selected="selected" disabled="disabled">Select an Option</option>
     <option value="0">Reports</option>
     <option value="1">Ratings</option>

    <script type="text/javascript" language="JavaScript">
    document.write('<select class="form" name="rnr" id="rnr" style="height:26px;"><option value="">Select an option</option></select>')
    </script>
    <noscript>
    <select class="form" name="rnr" id="rnr" style="height:22px; "></select>
    </noscript>

Ok, I figured this one out on my own and here is what I did for anyone that faces this problem themselves:

    <script>
     var rnr_prices= new Array();
         rnr_prices["1"]=40;
         rnr_prices["10"]=350;
         rnr_prices["25"]=700;
         rnr_prices["50"]=1200;
         rnr_prices["100"]=2000;
         rnr_prices["249"]=750;
         rnr_prices["499"]=1350;
         rnr_prices["249"]=750;
         rnr_prices["1000"]=2000;
         rnr_prices["100000"]=4000;

     function getDropPrice()
     {
        var dropPrices=0;
        var theForm = document.forms["form"];
        var selectedtype = theForm.elements["rnr"];

        dropPrices = rnr_prices[selectedtype.value];

        return dropPrices;
     }
   </script>

The solution was found here: http://www.javascript-coder.com/javascript-form/javascript-calculator-script.phtml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM