2017-04-25 87 views
0

我无法弄清楚为什么在点击“计算支付”后,显示为NaN。我认为这可能发生的唯一原因是因为computeMonthlyPayment()函数,更具体地说是计算线。我也认为它可能与单选按钮有关。显示NaN的文本框,以及基本计算问题

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

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

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

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

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

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

 
    </form> 
 
    </body> 
 
    </html>

回答

0

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

年的价值是"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>

+0

谢谢您的回答,但即使改变这种后,我仍然得到同样的问题。 –

+0

那么你把所有的值都包含在引号中? – epascarello

+0

这是正确的 –