在javascript存款/取款能力中出现银行余额

时间:2017-04-11 04:30:56

标签: javascript function

我有一个银行余额文本字段,我想根据存款/取款不断更新。这是我的代码:

<label>Bank Balance:</label>
<br />
<input type="text" id="bankBalance" value="1000" />
<br />
<br />
<button id="deposit">Deposit</button>
<button id="withdraw">Withdraw  </button>

var balance = document.getElementById('bankBalance').value;
balance = Number(balance);

var withdraw = document.getElementById('withdraw');

deposit.addEventListener('click', depositCash);

function depositCash () {
  var depositAmt = prompt('How much would you like to deposit?');
  depositAmt = Number(depositAmt);
  balance = depositAmt + balance;

  return balance;
}

未收到错误。为什么这不起作用?我将余额和depositAmt变量都转换为数字并添加它们。

谢谢!

1 个答案:

答案 0 :(得分:1)

你不能这样做:

document.getElementById('bankBalance').value = balance

您需要将值设置回文档元素,例如

<itunes:length></itunes:length>