0

我正在使用 Javascript 函数calculateTotal(),我收到一个$NaN直到所有字段都被选中。它应该显示每个选择的总数。谢谢你能给我的任何帮助。这是我的代码:

    var test1_prices= new Array();

    test1_prices["None"]=0;
        test1_prices["Intro A"]=22;

        test1_prices["Intro B"]=22;
        test1_prices["Intro C"]=22;
        test1_prices["T/one"]=22;
        test1_prices["T/two"]=22;
        test1_prices["T/three"]=22;
        test1_prices["RT"]=22;
        test1_prices["Frst/one"]=22;
        test1_prices["Frst/two"]=22;
        test1_prices["Frst/three"]=22;
        test1_prices["RTF"]=22;
        test1_prices["Scnd/one"]=22;
        test1_prices["Scnd/two"]=22;
        test1_prices["Scnd/three"]=22;
        test1_prices["RTTwo"]=22;
        test1_prices["Third/one"]=22;
        test1_prices["Third/two"]=22;
        test1_prices["Third/three"]=22;
        test1_prices["Forth/one"]=22;
        test1_prices["Forth/two"]=22;
        test1_prices["Forth/three"]=22;
        test1_prices["Western/Basic1"]=22;
        test1_prices["Western/Basic2"]=22;
        test1_prices["Western/Prim1"]=22;
        test1_prices["Western/Prim2"]=22;
        test1_prices["Western/Prim3"]=22;
        test1_prices["Western/Prim4"]=22;
        test1_prices["Other 15"]=22;

    var test2_prices= new Array();
        test2_prices["None"]=0;

        test2_prices["Intro A"]=22;
        test2_prices["Intro B"]=22;
        test2_prices["Intro C"]=22;
        test2_prices["T/one"]=22;
        test2_prices["T/two"]=22;
        test2_prices["T/three"]=22;
        test2_prices["RT"]=22;
        test2_prices["Frst/one"]=22;
        test2_prices["Frst/two"]=22;
        test2_prices["Frst/three"]=22;
        test2_prices["RTF"]=22;
        test2_prices["Scnd/one"]=22;
        test2_prices["Scnd/two"]=22;
        test2_prices["Scnd/three"]=22;
        test2_prices["RTTwo"]=22;
        test2_prices["Third/one"]=22;
        test2_prices["Third/two"]=22;
        test2_prices["Third/three"]=22;
        test2_prices["Forth/one"]=22;
        test2_prices["Forth/two"]=22;
        test2_prices["Forth/three"]=22;
        test2_prices["Western/Basic1"]=22;
        test2_prices["Western/Basic2"]=22;
        test2_prices["Western/Prim1"]=22;
        test2_prices["Western/Prim2"]=22;
        test2_prices["Western/Prim3"]=22;
        test2_prices["Western/Prim4"]=22;
        test2_prices["Other 15"]=22;

    function gettest1Price()
    {
    var test1Price=0;
    var theForm = document.forms["SchoolingForm"];
    var selectedtest1 = theForm.elements["test1"];
    test1Price = test1_prices[selectedtest1.value];

    return test1Price;
    }

   function gettest2Price()
   {
    var test2Price=0;
    var theForm = document.forms["SchoolingForm"];
    var selectedtest2 = theForm.elements["test2"];
    test2Price = test2_prices[selectedtest2.value];

    return test2Price;
    }




    function calculateTotal()
    {

    var Price = gettest1Price() + gettest2Price();

    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price For the Schooling Show $"+Price;

    }

    function hideTotal()
    {
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='none';
    }



<body onload='hideTotal()'>
    <div id="wrap">
        <form action="" id="SchoolingForm" onsubmit="return false;">
        <div>
            <div class="cont_order">
               <fieldset>
                <legend>Select Your Test</legend>
       <p>         
        <label >Test1</label>

        <select id="test1" name="test1" onchange="calculateTotal()">
          <option value="" title="" selected="selected">None</option>
          <option value="Intro A" title="Intro A $22" >Intro A $22</option>
          <option value="Intro B" title="Intro B $22" >Intro B $22</option>
          <option value="Intro C" title="Intro C $22" >Intro C $22</option>
          <option value="T/one" title="Training 1 $22" >Training 1 $22</option>
          <option value="T/two" title="Training 2 $22" >Training 2 $22</option>
          <option value="T/three" title="Training 3 $22" >Training 3 $22</option>
          <option value="RT" title="Rider Test Training $22" >Rider Test Training $22</option>
          <option value="Frst/one" title="First 1 $22" >First 1 $22</option>
          <option value="Frst/two" title="First 2 $22" >First 2 $22</option>
          <option value="Frst/three" title="First 3  $22" >First 3  $22</option>
          <option value="RTF" title="Rider Test First Level $22" >Rider Test First Level $22</option>
          <option value="Scnd/one" title="Second 1 $22" >Second 1 $22</option>
          <option value="Scnd/two" title="Second 2 $22" >Second 2 $22</option>
          <option value="Scnd/three2" title="Second 3 $22" >Second 3 $22</option>
          <option value="RTTwo" title="Rider Test Second Level $22" >Rider Test Second Level $22</option>
          <option value="Third/one" title="Third 1 $22" >Third 1 $22</option>
          <option value="Third/two" title="Third 2 $22" >Third 2 $22</option>
          <option value="Third/three" title="Third 3 $22" >Third 3 $22</option>
          <option value="Forth/one" title="Forth 1 $22" >Forth 1 $22</option>
          <option value="Forth/two" title="Forth 2 $22" >Forth 2 $22</option>
          <option value="Forth/three" title="Forth 3 $22" >Forth 3 $22</option>
          <option value="Western/Basic1" title="Western Basic 1" >Western Basic 1 $22</option>
          <option value="Western/Basic2" title="Western Basic 2" >Western Basic 2 $22</option>
          <option value="Western/Prim1" title="Western Primary 1" >Western Primary 1 $22</option>
          <option value="Western/Prim2" title="Western Primary 2" >Western Primary 2 $22</option>
          <option value="Western/Prim3" title="Western Primary 3" >Western Primary 3 $22</option>
          <option value="Western/Prim4" title="Western Primary 2" >Western Primary 4 $22</option>
          <option value="Other 15" title="Other $22" >Other $22</option>
               </select>
                <br/>
          </p>      
          <p>
           <label>Test2</label><br/>
        <select id="test2" name="test2" size="1"  onchange="calculateTotal()">
          <option value="" title="" selected="selected">&nbsp;</option>
          <option value="Intro A" title="Intro A $22" >Intro A $22</option>
          <option value="Intro B" title="Intro B $22" >Intro B $22</option>
          <option value="Intro C" title="Intro C $22" >Intro C $22</option>
          <option value="T/one" title="Training 1 $22" >Training 1 $22</option>
          <option value="T/two" title="Training 2 $22" >Training 2 $22</option>
          <option value="T/three" title="Training 3 $22" >Training 3 $22</option>
          <option value="RT" title="Rider Test Training $22" >Rider Test Training $22</option>
          <option value="Frst/one" title="First 1 $22" >First 1 $22</option>
          <option value="Frst/two" title="First 2 $22" >First 2 $22</option>
          <option value="Frst/three" title="First 3  $22" >First 3  $22</option>
          <option value="RTF" title="Rider Test First Level $22" >Rider Test First Level $22</option>
          <option value="Scnd/one" title="Second 1 $22" >Second 1 $22</option>
          <option value="Scnd/two" title="Second 2 $22" >Second 2 $22</option>
          <option value="Scnd/three2" title="Second 3 $22" >Second 3 $22</option>
          <option value="RTTwo" title="Rider Test Second Level $22" >Rider Test Second Level $22</option>
          <option value="Third/one" title="Third 1 $22" >Third 1 $22</option>
          <option value="Third/two" title="Third 2 $22" >Third 2 $22</option>
          <option value="Third/three" title="Third 3 $22" >Third 3 $22</option>
          <option value="Forth/one" title="Forth 1 $22" >Forth 1 $22</option>
          <option value="Forth/two" title="Forth 2 $22" >Forth 2 $22</option>
          <option value="Forth/three" title="Forth 3 $22" >Forth 3 $22</option>
          <option value="Western/Basic1" title="Western Basic 1" >Western Basic 1 $22</option>
          <option value="Western/Basic2" title="Western Basic 2" >Western Basic 2 $22</option>
          <option value="Western/Prim1" title="Western Primary 1" >Western Primary 1 $22</option>
          <option value="Western/Prim2" title="Western Primary 2" >Western Primary 2 $22</option>
          <option value="Western/Prim3" title="Western Primary 3" >Western Primary 3 $22</option>
          <option value="Western/Prim4" title="Western Primary 2" >Western Primary 4 $22</option>
          <option value="Other 15" title="Other $22" >Other $22</option>
        </select>

          </p> 

                <div id="totalPrice"></div>

                </fieldset>
            </div>

            <div class="cont_details">
                <fieldset>
                <legend>Contact Details</legend>
                <label for='name'>Name</label>
                <input type="text" id="name" name='name' />
                <br/>
                <label for='address'>Address</label>
                <input type="text" id="address" name='address' />
                <br/>
                <label for='phonenumber'>Phone Number</label>
                <input type="text"  id="phonenumber" name='phonenumber'/>
                <br/>
                </fieldset>
            </div>
            <input type='submit' id='submit' value='Submit' onclick="calculateTotal()" />
        </div>  
       </form>
    </div><!--End of wrap-->

</body>
4

2 回答 2

0

这有效:

function calculateTotal()
{
    var price1, price2 = 0;

    if (!isNaN(gettest1Price())) {
        price1 = gettest1Price();
    }

    if (!isNaN(gettest2Price())) {
        price2 = gettest2Price();
    }

    var Price = price1 + price2;

    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price For the Schooling Show $"+Price;

}

还需要更改 HTML,你有value=""

<option value="None" title="" selected="selected">None</option>

这是工作小提琴:

http://jsfiddle.net/8aBBR/1/

于 2013-02-21T14:55:06.430 回答
0

问题是你的初始化test1_pricestest2_prices数组,它们必须是一个对象{}才能接收一个字符串作为键

于 2013-02-21T14:44:58.733 回答