Javascript函数calculateTotal()显示$NaN,直到选中所有字段

Javascript函数calculateTotal()显示$NaN,直到选中所有字段,javascript,function,calculated-field,Javascript,Function,Calculated Field,我正在使用Javascript函数calculateTotal(),我收到一个$NaN,直到所有字段都被选中。它应该显示每个选择的总数。谢谢你能给我的任何帮助。这是我的密码: var test1_prices= new Array(); test1_prices["None"]=0; test1_prices["Intro A"]=22; test1_prices["Intro B"]=22; test1_prices["Int

我正在使用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>
var test1_prices=new Array();
测试1_价格[“无”]=0;
测试1_价格[“简介A”]=22;
测试1_价格[“简介B”]=22;
测试1_价格[“简介C”]=22;
测试1_价格[“T/one”]=22;
测试1_价格[“T/two”]=22;
测试1_价格[“T/three”]=22;
测试1_价格[“RT”]=22;
测试1_价格[“第一次/一次]=22;
测试1_价格[“第一个/两个”]=22;
测试1_价格[“第一/三”]=22;
测试1_价格[“RTF”]=22;
测试1_价格[“Scnd/一”]=22;
测试1_价格[“Scnd/two”]=22;
测试1_价格[“Scnd/three”]=22;
测试1_价格[“RTTwo”]=22;
测试1_价格[“第三/一”]=22;
测试1_价格[“第三/两”]=22;
测试1_价格[“第三/三”]=22;
测试1_价格[“第四/一”]=22;
测试1_价格[“第四/第二”]=22;
测试1_价格[“四/三”]=22;
测试1_价格[“西部/基础1”]=22;
测试1_价格[“西部/基础2”]=22;
测试1_价格[“西部/Prim1”]=22;
测试1_价格[“西部/Prim2”]=22;
测试1_价格[“西部/普里姆3”]=22;
测试1_价格[“西部/普里姆4”]=22;
测试1_价格[“其他15”]=22;
var test2_prices=新数组();
测试2_价格[“无”]=0;
测试2_价格[“简介A”]=22;
测试2_价格[“简介B”]=22;
测试2_价格[“简介C”]=22;
测试2_价格[“T/one”]=22;
测试2_价格[“T/two”]=22;
测试2_价格[“T/three”]=22;
测试2_价格[“RT”]=22;
测试2_价格[“第一次/一次]=22;
测试2_价格[“第一个/两个”]=22;
测试2_价格[“第一/三”]=22;
测试2_价格[“RTF”]=22;
测试2_价格[“Scnd/一”]=22;
测试2_价格[“Scnd/2”]=22;
测试2_价格[“Scnd/three”]=22;
测试2_价格[“RTTwo”]=22;
测试2_价格[“第三/一”]=22;
测试2_价格[“第三/两”]=22;
测试2_价格[“第三/三”]=22;
测试2_价格[“第四/一”]=22;
测试2_价格[“第四/第二”]=22;
测试2_价格[“四/三”]=22;
测试2_价格[“西部/基础1”]=22;
测试2_价格[“西部/基本2”]=22;
测试2_价格[“西部/普里姆1”]=22;
测试2_价格[“西部/Prim2”]=22;
测试2_价格[“西部/普里姆3”]=22;
测试2_价格[“西部/普里姆4”]=22;
测试2_价格[“其他15”]=22;
函数gettest1Price()
{
var test1Price=0;
var theForm=document.forms[“SchoolingForm”];
var selectedtest1=form.elements[“test1”];
test1价格=test1价格[selectedtest1.value];
退货价格;
}
函数gettest2Price()
{
var test2Price=0;
var theForm=document.forms[“SchoolingForm”];
var selectedtest2=form.elements[“test2”];
test2Price=test2_价格[selectedtest2.value];
返回测试价格;
}
函数计算器总计()
{
var Price=gettest1Price()+gettest2Price();
var divobj=document.getElementById('totalPrice');
display='block';
divobj.innerHTML=“学校教育展的总价$”+价格;
}
函数hideTotal()
{
var divobj=document.getElementById('totalPrice');
display='none';
}
选择您的测试

测试1
没有一个
介绍A$22
简介B$22
简介C$22
培训1美元22
培训2美元22
培训3美元22
骑手测试训练$22
首张$22
首两张$22
前3张22美元
骑手测试一级$22
第二个1美元22
第二个2美元22
第二个3美元22
骑手测试二级$22
第三个1美元22
第三个2美元22
第三个3美元22美元
四分之一22美元
2美元22美分
第三张22美元
西部基础1美元22
西部基础2美元22
西区小一22元
西区小学2元22分
西区小三22元
西区小四22元
其他22美元

测试2
介绍A$22 简介B$22 简介C$22 培训1美元22 培训2美元22 培训3美元22 骑手测试训练$22 首张$22 首两张$22 前3张22美元 骑手测试一级$22 第二个1美元22 第二个2美元22 第二个3美元22 骑手测试二级$22 第三个1美元22 第三个2美元22 第三个3美元22美元 四分之一22美元 2美元22美分 第三张22美元 西部基础1美元22 西部基础2美元22 西区小一22元 西区小学2元22分 西区小三22元 西区小四22元 其他22美元

联系方式 名称
地址
电话号码

问题是您将
test1\u prices
test2\u prices
初始化为数组,它们必须是对象
{}
才能接收字符串作为键

此操作:

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=”“

这是工作小提琴:


我认为这无关紧要。也许这确实是一个更好的练习,但结果是一样的,我想。你能把这个放到JSFIDLE上吗?我不这么认为
<option value="None" title="" selected="selected">None</option>