関数に価格を渡すことによってJavascriptで合計を作成しようとしています。実行中の合計を維持するための関数へのパラメータの受け渡し
ユーザーがボタンを押すと、価格がaddBalance関数に渡されます。私は実行中の合計を追加して表示する方法がわかりません。
私はスーパースタックです。助けてください。
// JavaScript Document
document.getElementById("computing").addEventListener('click', function() {
"use strict";
bookOrder("Computing");
}, false);
document.getElementById("english").addEventListener('click', function() {
"use strict";
bookOrder("English");
}, false);
document.getElementById("maths").addEventListener('click', function() {
"use strict";
bookOrder("Maths");
}, false);
document.getElementById("geography").addEventListener('click', function() {
"use strict";
bookOrder("Geography");
}, false);
document.getElementById("history").addEventListener('click', function() {
"use strict";
bookOrder("History");
}, false);
document.getElementById("science").addEventListener('click', function() {
"use strict";
bookOrder("Science");
}, false);
document.getElementById("showArray").addEventListener('click', showArrayValues);
var books = []; //array
//user defined function & passing parameters
function bookOrder(book) {
"use strict";
//conditional statement
switch (book) {
case "Computing":
books.push("<p>Computing - £6.99</p>");
addBalance(6.99);
alert("Computing - £6.99, has been added to your order");
break;
case "English":
books.push("<p>English - £7.99</p>");
addBalance(7.99);
alert("English - £7.99, has been added to your order");
break;
case "Maths":
books.push("<p>Maths - £8.99</p>");
addBalance(8.99);
alert("Maths - £8.99, has been added to your order");
break;
case "Geography":
books.push("<p>Geography - £9.99</p>");
addBalance(9.99);
alert("Geography - £9.99, has been added to your order");
break;
case "History":
books.push("<p>History - £10.99</p>");
addBalance(10.99);
alert("History - £10.99, has been added to your order");
break;
case "Science":
books.push("<p>Science - £11.99</p>");
addBalance(11.99);
alert("Science - £11.99, has been added to your order");
break;
}
}
function showArrayValues() {
"use strict";
document.getElementById("output").innerHTML = "";
for (var i in books) //loop
{ //manipulating the DOM
document.getElementById("output").innerHTML += books[i];
}
}
function addBalance(param1) {
"use strict";
parseFloat(document.getElementById("total").innerHTML += param1);
}
を参照してください。その後、書籍の配列を横断して、いつでも合計を表示できます。 – NDFA