2017-04-25 73 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

這是正確的 –