我的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);
}
答案 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