文本框显示NaN,并发出基本计算问题

时间:2017-04-25 00:04:15

标签: javascript html radio-button nan

我无法弄明白为什么在我点击"计算付款后#34;它显示NaN。我认为可能发生这种情况的唯一原因是因为computeMonthlyPayment()函数,更具体地说是计算行。我也认为它可能与单选按钮有关。



 <!DOCTYPE html>
    <html>
    <head>
	<title> Monthly Payment Calculator</title>
    </head>

    <script>
	function computeMonthlyPmt(){
		var amt = document.MonthlyPmt.LoanAmt.value;
		var r = document.MonthlyPmt.Rate.value;
		var t = document.MonthlyPmt.Year.value;
		document.MonthlyPmt.Payment.value = (amt*(r/12))/(1-Math.pow(1+(r/12),-12*t));
		
	}
    </script>

    <body>
    <h1> Monthly Payment Calculator </h1>

    <form name="MonthlyPmt">
	<p> Enter Loan:<input type="text" name="LoanAmt" value="" /> </p>
	
		
	<p> Select Rate: </p>
	<select name="Rate">
		<option value=0.04>4%</option>
		<option value=0.045>4.5%</option>
		<option value=0.05>5%</option>
		<option value=0.055>5.5%</option>
		<option value=0.06>6%</option>
	</select>

	<p> Select Term: </p> 
		<input type="radio" name="Year" value=10/>10 Years<br>
		<input type="radio" name="Year" value=15/>15 Years<br>
		<input type="radio" name="Year" value=30/>30 Years<br>

	<p> Monthly Payment: </p>
		<input type="text" name="Payment" value="" /><br>
		<input type="button" value="Compute Payment" name="btnCompute" onclick="computeMonthlyPmt()" />

    </form>
    </body>
    </html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在属性周围使用引号!是的,它们可以是可选的,但它在这里位于后方。

年份的值为"30/"而不是30

使用以下命令调试它:

console.log(amt, r, t)

&#13;
&#13;
function computeMonthlyPmt() {
  var amt = document.MonthlyPmt.LoanAmt.value;
  var r = document.MonthlyPmt.Rate.value;
  var t = document.MonthlyPmt.Year.value;
  document.MonthlyPmt.Payment.value = (amt * (r / 12)) / (1 - Math.pow(1 + (r / 12), -12 * t));

}
&#13;
<form name="MonthlyPmt">
  <p> Enter Loan:<input type="text" name="LoanAmt" value="" /> </p>


  <p> Select Rate: </p>
  <select name="Rate">
    <option value="0.04">4%</option>
    <option value="0.045">4.5%</option>
    <option value="0.05">5%</option>
    <option value="0.055">5.5%</option>
    <option value="0.06">6%</option>
</select>

  <p> Select Term: </p>
  <input type="radio" name="Year" value="10" />10 Years<br>
  <input type="radio" name="Year" value="15" />15 Years<br>
  <input type="radio" name="Year" value="30" />30 Years<br>

  <p> Monthly Payment: </p>
  <input type="text" name="Payment" value="" /><br>
  <input type="button" value="Compute Payment" name="btnCompute" onclick="computeMonthlyPmt()" />

</form>
&#13;
&#13;
&#13;