Javascript计算表单

Javascript计算表单,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试为一个学校班级创建一个HTML和Javascript计算表单。我拥有的当前代码允许表单在单击radio/复选框时自动计算总成本。问题是,我需要以某种方式使它,无论是,1。如果用户选择了单选按钮/复选框或2,我可以得到某种实时检查。转换代码,以便它需要一个提交和重置按钮来进行验证并给出最终成本。我目前拥有的是: HTML: 假期计算表 霍珀岛邮轮票价计算器 填写下表以计算您的邮轮费用。 主海滩-阿兹卡班岛(49美元) 主海滩-爱迪岛(79美元) 主海滩-金银岛(109美元) 主海

我正在尝试为一个学校班级创建一个HTML和Javascript计算表单。我拥有的当前代码允许表单在单击radio/复选框时自动计算总成本。问题是,我需要以某种方式使它,无论是,1。如果用户选择了单选按钮/复选框或2,我可以得到某种实时检查。转换代码,以便它需要一个提交和重置按钮来进行验证并给出最终成本。我目前拥有的是: HTML:


假期计算表
霍珀岛邮轮票价计算器
填写下表以计算您的邮轮费用。



主海滩-阿兹卡班岛(49美元)
主海滩-爱迪岛(79美元)
主海滩-金银岛(109美元)
主海滩-吉利根岛(89美元)
主海滩-骷髅岛(59美元)


双程票

经济舱座位
商务舱座位
头等舱座位

Javascript:

var holiday_cost = new Array();
 holiday_cost["Beach1"]=49;
 holiday_cost["Beach2"]=79;
 holiday_cost["Beach3"]=109;
 holiday_cost["Beach4"]=89;
 holiday_cost["Beach5"]=59;

 var fare_prices = new Array();
 fare_prices["Flight1"]=1;
 fare_prices["Flight2"]=1.5;
 fare_prices["Flight3"]=2.5;


function getHolidayPrice()
{  
    var HolidayPrice=1;

    var theForm = document.forms["holidayform"];

    var selectedBeach = theForm.elements["selectedbeach"];

    for(var i = 0; i < selectedBeach.length; i++)
    {

        if(selectedBeach[i].checked)
        {

            HolidayPrice = holiday_cost[selectedBeach[i].value];

            break;
        }
    }

    return HolidayPrice;
}



function twowayPrice()
{
    var twowayPrice=1;

    var theForm = document.forms["holidayform"];

    var includeTwoway = theForm.elements["twowayfare"];


    if(includeTwoway.checked==true)
    {
        twowayPrice=2;
    }

    return twowayPrice;
}

function getSeatPrice()
{  
    var SeatPrice=1;

    var theForm = document.forms["holidayform"];

    var selectedSeat = theForm.elements["selectedseat"];

    for(var i = 0; i < selectedSeat.length; i++)
    {

        if(selectedSeat[i].checked)
        {

            SeatPrice = fare_prices[selectedSeat[i].value];

            break;
        }
    }

    return SeatPrice;
}



function calculateTotal()
{
    //Here we get the total price by calling our function
    //Each function returns a number so by calling them we add the values they return together
    var flightPrice = getHolidayPrice() * twowayPrice() * getSeatPrice();

    //display the result
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price For the Holiday $"+flightPrice;

}

function hideTotal()
{
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='none';
}
var holiday_cost=new Array();
假日费用[“海滩1”]=49;
假日费用[“海滩2”]=79;
假日费用[“海滩3”]=109;
假日费用[“海滩4”]=89;
假日费用[“海滩5”]=59;
var fare_prices=新数组();
票价[“航班1”]=1;
票价[“航班2”]=1.5;
票价[“航班3”]=2.5;
函数getHolidayPrice()
{  
var HolidayPrice=1;
var theForm=document.forms[“holidayform”];
var selectedBeach=form.elements[“selectedBeach”];
对于(变量i=0;i
请帮忙!?,
Fonza

您对该代码有什么问题?不要在单击收音机时执行任何操作,请使用“提交”按钮进行计算。为进行验证,请检查所选收音机(如果未找到),然后显示某种错误消息。你被困在哪里?
var holiday_cost = new Array();
 holiday_cost["Beach1"]=49;
 holiday_cost["Beach2"]=79;
 holiday_cost["Beach3"]=109;
 holiday_cost["Beach4"]=89;
 holiday_cost["Beach5"]=59;

 var fare_prices = new Array();
 fare_prices["Flight1"]=1;
 fare_prices["Flight2"]=1.5;
 fare_prices["Flight3"]=2.5;


function getHolidayPrice()
{  
    var HolidayPrice=1;

    var theForm = document.forms["holidayform"];

    var selectedBeach = theForm.elements["selectedbeach"];

    for(var i = 0; i < selectedBeach.length; i++)
    {

        if(selectedBeach[i].checked)
        {

            HolidayPrice = holiday_cost[selectedBeach[i].value];

            break;
        }
    }

    return HolidayPrice;
}



function twowayPrice()
{
    var twowayPrice=1;

    var theForm = document.forms["holidayform"];

    var includeTwoway = theForm.elements["twowayfare"];


    if(includeTwoway.checked==true)
    {
        twowayPrice=2;
    }

    return twowayPrice;
}

function getSeatPrice()
{  
    var SeatPrice=1;

    var theForm = document.forms["holidayform"];

    var selectedSeat = theForm.elements["selectedseat"];

    for(var i = 0; i < selectedSeat.length; i++)
    {

        if(selectedSeat[i].checked)
        {

            SeatPrice = fare_prices[selectedSeat[i].value];

            break;
        }
    }

    return SeatPrice;
}



function calculateTotal()
{
    //Here we get the total price by calling our function
    //Each function returns a number so by calling them we add the values they return together
    var flightPrice = getHolidayPrice() * twowayPrice() * getSeatPrice();

    //display the result
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price For the Holiday $"+flightPrice;

}

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