初學者Javascript學生,並堅持在希望簡單的東西。Javascript函數打印到相同的跨度元素
我有兩個JavaScript函數可以按預期工作,但它們都打印到相同的Span元素,即使它們具有不同的ID。
在SO或Google的任何地方找不到解決方案。
對不起,這是很多的代碼,不知道如何顯示它。
https://jsfiddle.net/KrnRbts/dmeztkr0/
任何幫助是非常讚賞,歡呼聲如此!
JAVASCRIPT 1:
function calculateHeliSubTotal() {
var adultCountHeli = document.getElementById("adultCountHelicopter"),
childCountHeli = document.getElementById("childCountHelicopter"),
infantCountHeli = document.getElementById("infantCountHelicopter"),
adultCostHeli = 230,
childCostHeli = 160,
infantCostHeli = 0
showSpan = document.getElementById('subTotalHelicopter');
//changes the sub-total if the User changes the adult quantity
function runAdultCountHeliEvent() {
adultCountHeli.addEventListener('change', function() {
getHeliSubTotal();
});
}
//changes the sub-total if the User changes the child quantity
function runChildCountHeliEvent() {
childCountHeli.addEventListener('change', function() {
getHeliSubTotal();
});
}
//changes the sub-total if the User changes the infant quantity
function runInfantCountHeliEvent() {
infantCountHeli.addEventListener('change', function() {
getHeliSubTotal();
});
}
//calls to run the functions
getHeliSubTotal();
//a function that is used to run all of the events
function runHeliEvents() {
runAdultCountHeliEvent();
runChildCountHeliEvent();
runInfantCountHeliEvent();
}
//calls to run the functions
runHeliEvents();
//calculates the subtotal for the current service
function getHeliSubTotal() {
var adultHeliSubTotal = adultCountHeli.value * adultCostHeli,
childHeliSubTotal = childCountHeli.value * childCostHeli,
infantHeliSubTotal = infantCountHeli.value * infantCostHeli
heliSubTotal = adultHeliSubTotal + childHeliSubTotal + infantHeliSubTotal;
showSpan.innerHTML = '$' + heliSubTotal + ' ' + '(ex-GST)';
}
}
calculateHeliSubTotal();
JAVASCRIPT 2: - 實際上是相同的,但不同的變種值。
function calculateFWSubTotal() {
var adultCountFW = document.getElementById("adultCountFixedWing"),
childCountFW = document.getElementById("childCountFixedWing"),
infantCountFW = document.getElementById("infantCountFixedWing"),
adultCostFW = 530,
childCostFW = 260,
infantCostFW = 0
showSpan = document.getElementById('subTotalFixedWing');
//changes the sub-total if the User changes the adult quantity
function runAdultCountFWEvent() {
adultCountFW.addEventListener('change', function() {
getFWSubTotal();
});
}
//changes the sub-total if the User changes the child quantity
function runChildCountFWEvent() {
childCountFW.addEventListener('change', function() {
getFWSubTotal();
});
}
//changes the sub-total if the User changes the infant quantity
function runInfantCountFWEvent() {
infantCountFW.addEventListener('change', function() {
getFWSubTotal();
});
}
//calls to run the functions
getFWSubTotal();
//a function that is used to run all of the events
function runFWEvents() {
runAdultCountFWEvent();
runChildCountFWEvent();
runInfantCountFWEvent();
}
//calls to run the functions
runFWEvents();
//calculates the subtotal for the current service
function getFWSubTotal() {
var adultFWSubTotal = adultCountFW.value * adultCostFW,
childFWSubTotal = childCountFW.value * childCostFW,
infantFWSubTotal = infantCountFW.value * infantCostFW
FWSubTotal = adultFWSubTotal + childFWSubTotal + infantFWSubTotal;
showSpan.innerHTML = '$' + FWSubTotal + ' ' + '(ex-GST)';
}
}
calculateFWSubTotal();
*「對不起,這是很多代碼,不知道如何顯示它。」*嘗試。請參閱:[MCVE] –
showSpan是全局變量,儘量this.showSpan –
@MarekJanoud可惜沒,都還算到第二SPAN元素,但由於配置 – KrnRbts