为什么我的Javascript价格计算器不起作用?

时间:2013-10-30 00:27:38

标签: javascript html

我无法使下面的脚本工作,我真的不明白为什么。应该工作正常

每个变量都会被考虑,但是没有价格出现?

可在此页面上查看HTML:http://virkfilm.dk/prisb/

 var cake_prices = new Array();
 cake_prices["filmtype1"]=5000;
 cake_prices["filmtype2"]=5000;
 cake_prices["filmtype3"]=5000;


 var filling_prices= new Array();
 filling_prices["Canon"]=0;
 filling_prices["Blackmagic"]=2000;
 filling_prices["Red"]=5000;

  var rejse_prices= new Array();
 rejse_prices["storkbh"]=0;
 rejse_prices["sjaelland"]=300;
 rejse_prices["fyn"]=600;
 rejse_prices["jylland"]=1000;

  var dage_prices= new Array();
 dage_prices["1"]=0;
 dage_prices["2"]=3500;
 dage_prices["3"]=7000;


  var speak_prices= new Array();
 speak_prices["nospeak"]=0;
 speak_prices["dansk"]=1600;
 speak_prices["engelsk"]=1600;



function getCakeSizePrice()
{  
    var cakeSizePrice=0;
    var theForm = document.forms["prisberegner"];
    var selectedCake = theForm.elements["selectedcake"];
    for(var i = 0; i < selectedCake.length; i++)
    {
        if(selectedCake[i].checked)
        {
            cakeSizePrice = cake_prices[selectedCake[i].value];
            break;
        }
    }
    return cakeSizePrice;
}


function getFillingPrice()
{
    var cakeFillingPrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedFilling = theForm.elements["filling"];


    cakeFillingPrice = filling_prices[selectedFilling.value];

    return cakeFillingPrice;
}

function getRejsePrice()
{
    var RejsePrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedRejse = theForm.elements["rejse"];


    RejsePrice = rejse_prices[selectedRejse.value];

    return RejsePrice;
}

function getDagePrice()
{
    var DagePrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedDage = theForm.elements["dage"];


    DagePrice = dage_prices[selectedDage.value];

    return DagePrice;
}

function getSpeakPrice()
{
    var SpeakPrice=0;
    var theForm = document.forms["prisberegner"];
     var selectedDage = theForm.elements["speak"];


    SpeakPrice = speak_prices[selectedSpeak.value];

    return SpeakPrice;
}


function lydPrice()
{
    var lydPrice=0;
    var theForm = document.forms["prisberegner"];
    var lyd = theForm.elements["lyd"];
    if(lyd.checked==true)
    {
        lydPrice=1500;
    }
    return lydPrice;
}

function speakoverPrice()
{
    var speakoverPrice=0;
    var theForm = document.forms["prisberegner"];
    var speakover = theForm.elements["speakover"];
    if(speakover.checked==true)
    {
        speakoverPrice=1600;
    }
    return speakoverPrice;
}

function uspeakPrice()
{
    var uspeakPrice=0;
    var theForm = document.forms["prisberegner"];
    var uspeak = theForm.elements["uspeak"];
    if(uspeak.checked==true)
    {
        uspeakPrice=1000;
    }
    return uspeakPrice;
}

function musikPrice()
{

    var musikPrice=0;
    var theForm = document.forms["prisberegner"];
    var musik = theForm.elements["musik"];
    if(musik.checked==true){
        musikPrice=1200;
    }
    return musikPrice;
}


function storyPrice()
{

    var musikPrice=0;
    var theForm = document.forms["prisberegner"];
    var story = theForm.elements["story"];
    if(story.checked==true){
        storyPrice=1000;
    }
    return storyPrice;
}

function includecandlesPrice()
{

    var includecandlesPrice=0;
    var theForm = document.forms["prisberegner"];
    var includecandles = theForm.elements["includecandles"];
    if(includecandles.checked==true){
        includecandlesPrice=1000;
    }
    return includecandlesPrice;
}        

function calculateTotal()
{
    var cakePrice = getCakeSizePrice() + getSpeakPrice() + getFillingPrice() + getRejsePrice() + getDagPrice() + lydPrice() + speakoverPrice() + uspeakPrice() + musikPrice() + storyPrice() + includecandlesPrice();

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

}

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

我应该感谢javascript-coder.com我试图改编的脚本模板。

2 个答案:

答案 0 :(得分:1)

SpeakPrice = speak_prices[selectedSpeak.value];

未定义selectedSpeak变量。根据您的要求定义它。

答案 1 :(得分:1)

你在哪里

var selectedDage = theForm.elements["speak"];

应该是

var selectedSpeak = theForm.elements["speak"];

否则selectedSpeak未在任何地方定义。