javascript抵押计算器

时间:2016-03-19 01:30:21

标签: javascript css html5

我有一个关于建立这个抵押贷款计算器的快速问题。我是javascript的新手,但有点熟悉其他编程语言。当我输入principal,termOfLoan和APR的值然后按下计算按钮时,没有任何反应。谁能简单地指导我进入我所缺少的东西?这应该有一个我忽略的快速解决方案。提前谢谢。

<title>Mortgage Calculator</title>
<meta charset = "UTF-8">
<link rel = "stylesheet" href = "mortgageCalculator.css">

<script>

    function calculatePayment() {


        var principal = parseFloat(document.getElementById("principal").value);
        //principal = parseInt(principal);

        var termOfLoan = parseFloat(document.getElementById("termOfLoan").value);
        //termOfLoan = parseInt(termOfLoan);

        var annualInterestRate = parseFloat(document.getElementById("annualInterestRate").value);
        //annualInterestRate = parseFloat(annualInterestRate);
        //document.getElementById("Calculate").value = principal * annualInterestRate / (1 -(Math.pow(1 / (1 + annualInterestRate) , termOfLoan)));
        //var monthlyPayment = principal * annualInterestRate / (1 -(Math.pow(1 / (1 + annualInterestRate) , termOfLoan))); 

        var percentageRate = annualInterestRate / 1200;
        var lengthOfLoan = 12 * termOfLoan;
        var monthlyPayment = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))));
        monthlyPayment = monthlyPayment.toFixed(2);


        document.getElementById("payment").innerHTML = monthlyPayment;

        //document.getElementById('monthlyPayment').innerHTML = "Monthly Payment" + monthlyPayment;


    }


</script>

<h1>Mortgage Calculator</h1>
<p>Use this simple home mortgage calculator to calculate monthly payments on mortgage rates. Payment examples shown do not include amounts for taxes and insurance premiums. 
*Note: please contact your financial insitution for final monthy payments, as the can and will vary from lender to lender. This calculator should only be used for comparison purposes</p>
<form>
<table>

    <tr>
        <td class = "labels">Principal Loan Amount</td>
        <td class = "textbox"><input type = "text" id = "principal" onclick = "calculatePayment()"></td>
    </tr>
    <tr>
        <td class = "labels">Mortgage Period (years)</td>
        <td class = "textbox"><input type = "text" id = "termOfLoan" onclick = "calculatePayment()"></td>
    </tr>
    <tr>
        <td class = "labels">Annual Interest Rate</td>
        <td class = "textbox"><input type = "text" id = "annualInterestRate" onclick = "calculatePayment()"></td>
    </tr>
    <tr>
        <td class = "labels">Monthly Mortgage Payment</td>
        <td class = "textbox"><input type = "number" id = "payment" name = "monthlyPayment"></td>

    </tr>
    <tr>
        <td class = "button"><button onclick = "calculatePayment()">Calculate</button></td>
        <td class = "button"><input type = "reset" name = "Reset"></td>
    </tr>


</table>
</form>

1 个答案:

答案 0 :(得分:1)

工作示例https://jsfiddle.net/w4an90nr/

付款是输入代码,因此您必须使用value而不是innerHTML

document.getElementById("payment").value = monthlyPayment; //value not innerHTML

此处还有一个错误的右括号)。删除它

var monthlyPayment = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)));