【问题标题】:Textbox showing NaN, and issue with basic calculation显示 NaN 的文本框,以及基本计算的问题
【发布时间】:2017-09-21 19:22:26
【问题描述】:

我不明白为什么在我点击“计算付款”后它显示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>

【问题讨论】:

    标签: javascript html radio-button nan


    【解决方案1】:

    在你的属性周围使用引号!是的,它们可以是可选的,但它在后面咬你。

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

    调试它:

    console.log(amt, r, t)
    

    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));
    
    }
    <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>

    【讨论】:

    • 感谢您的回答,但即使更改后,我仍然遇到同样的问题。
    • 所以你把所有的值都用引号括起来了?
    • 我还注意到我的 标签在 标签之后
    • 我刚刚测试过它并且工作正常。你在第一个文本框中输入了什么?
    • 价值为 300,000 美元、4% 和 30 年
    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多