添加功能总数

时间:2015-04-28 08:14:40

标签: javascript

我的total_price功能不起作用。我想把它放在我已经做过的totalPrice段落上,但它没有显示。

function calcAdult()
{
    var ticketPrice = 12;
    var amount = document.getElementById('SAinput').value;
    var total = ticketPrice * amount;
    document.getElementById('SAPrice').innerHTML = "Cost = $" +total.toFixed(2);
    return total;
} 

function calcCon()
{
    var ticketPrice = 10;
    var amount = document.getElementById('SPinput').value;
    var total = ticketPrice * amount;
    document.getElementById("SPPrice").innerHTML = "Cost = $" +total.toFixed(2);
    return total;
}   
function total_price()
{
    var price = calcAdult() + calcCon();
    document.getElementById("totalPrice").innerHTML = "Cost = $" + price.toFixed(2);            
}

1 个答案:

答案 0 :(得分:0)

根据讨论中的评论,您根本没有调用total_price方法,而是在更改字段值时调用其他2种方法。

相反,您可以在更改值时调用total_price方法,例如

<input id="SAinput" onchange="total_price()"/>
<input id="SPinput" onchange="total_price()"/>

然后

function calcAdult() {
    var ticketPrice = 12;
    var amount = document.getElementById('SAinput').value;
    var total = (ticketPrice * amount) || 0;
    document.getElementById('SAPrice').innerHTML = "Cost = $" + total.toFixed(2);
    return total;
}

function calcCon() {
    var ticketPrice = 10;
    var amount = document.getElementById('SPinput').value;
    var total = (ticketPrice * amount) || 0;
    document.getElementById("SPPrice").innerHTML = "Cost = $" + total.toFixed(2);
    return total;
}

function total_price() {
    var price = calcAdult() + calcCon();
    document.getElementById("totalPrice").innerHTML = "Cost = $" + price.toFixed(2);

}

演示:Fiddle