對不起,如果這是一個愚蠢的問題。我是編程的新手... 我從文本輸入有3個值。我希望將這些值中的2個存儲到單獨的數組中以供以後訪問。最後我想在文檔中顯示這3個值的總和。 我在做什麼錯?在javascript中添加HTML文本框的值時的困難
這裏是我的代碼:
<script>
function displayCurrentBalance() {
var initialBalance = parseFloat(document.getElementById("initialBalance").value);
var inAmounts=[0];
var outAmounts = [0];
inAmounts.push(document.getElementById("amountIn").value);
outAmounts.push(document.getElementById("amountOut").value);
var sumIn = (inAmounts.reduce(function(a, b) {
return a + b[1];
}, 0));
var sumOut = (outAmounts.reduce(function(c, d) {
return c + d[1];
}, 0));
var result = initialBalance + sumIn - sumOut;
document.write(result);
};
displayCurrentBalance();
</script>
關閉你的變量語句...'var variable ='';'有幾次你忘記了這麼做 –
修復了它.still不工作,但是謝謝 – Kaitis
你可以把它放在一個jsFiddle中,所有的代碼? – Evan