无法弄清楚为什么这并没有给我任何输出值

时间:2014-10-22 16:27:13

标签: javascript html

我对JavaScript和编程非常陌生。有人可以向我解释为什么这不起作用吗?

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
function computeLoan(){
    var amount = document.getElementById('amount').value;
    var interest_rate = document.getElementById('interest_rate').value;
    var months = document.getElementById('months').value;
    var interest = (amount * (interest_rate * .01)) / months;
    var payment = ((amount / months) + interest).toFixed(2);
    payment = payment.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    document.getElementById('payment').innerHTML = "Monthly Payment = $"+payment;
}
    </script>
</head>
<body>
<p>Loan Amount: $<input id="amount" type="number" min="1" max="1000000"                 onchange="computeLoan()"></p>
<p>Interest Rate: <input id="interest_rate" type="number" min="0" max="100" value="10" step=".1" onchange="computeLoan()">%</p>
<p>Months: <input id="months" type="number" min="1" max="72" value="1"                  step="1" onchange="computeLoan()"></p
<h2 id="payment"></h2>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

月份 - 您在结束<p>标记上缺少括号。这导致您的<h2>未定义

<p>Months: <input id="months" type="number" min="1" max="72" value="1" step="1" onchange="computeLoan()"></p

</p

最后是你的问题。