1

我正在开发一个功能,该功能可以在我使用“提示”获取输入时进行基本的贷款摊销。从那个到使用表格,有些事情不太对劲。我正在尝试将同一张表打印到 div 区域,但目前根本无法获得它。我敢肯定这只是我的菜鸟眼睛,但我看不出我在这个上哪里错了....

感谢您花时间帮助平民。

html -

 <!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Loan Amortization</title>
<script type="text/javascript" src="LoanAmortization.js"></script>
</head>

<body>
    <h1>Loan Amortization</h1>


    <fieldset>
        <legend>Inputs</legend>
        <form>
            Principal: <input type="text" id="principalInput" /><br />
            Interest:  <input type="text" id="interestInput" /><br />
            Terms:     <select id="termsInput">     

                    <option>12 months</option>
                    <option>24 months</option>
                    <option>36 months</option>
                    <option>48 months</option>
                    <option>60 months</option>
            </select> <br />
            <input type="button" id="myButton" value="Click this!" 
            onclick="amort('principalInput','interestInput','termsInput', 
            'myDiv')" />
        </form>
    </fieldset>


    <fieldset>
        <legend>Outputs</legend>

    <div id="myDiv">


    </div>  
    </fieldset>

</body>

JS-

    function amort(principalInput, interestInput, termsInput)
{
    total = parseInt(document.getElementById(principalInput).value);
    var monthlyRate = parseFloat
    (document.getByElementId(interestInput).value)/12;
    var payment = total * (monthlyRate/(1-Math.pow(
        1+monthlyRate, -terms)));

    var result = "Loan amount: $" + principalInput.toFixed(2) +  "<br />" + 
        "Interest rate: " + (interestInput*100).toFixed(2) +  "%<br />" +
        "Number of months: " + termsInput + "<br />" +
        "Monthly payment: $" + payment.toFixed(2) + "<br />" +
        "Total paid: $" + (payment * terms).toFixed(2) + "<br /><br />";

    result += "<table border='1'><tr><th>Month</th><th>Balance</th>" + 
        "<th>Interest</th><th>Principal</th>";

    // insert your code here
    var i = 1;

    var interestPaid = (total * monthlyRate);
    var principalPaid = payment - interestPaid;

    while(i <= terms){
        result+= "<tr><td>" + i + "</td>";
        result+= "<td>" + total.toFixed(2) + "</td>";
        result+= "<td>" + interestPaid.toFixed(2) + "</td>";
        result+= "<td>" + principalPaid.toFixed(2)  + "</td>";
        result+= "</tr>";
        total = total - principalPaid;
        interestPaid = total * monthlyRate;
        principalPaid = payment - interestPaid;
        i++;
    }

    result += "</table>";
    document.getElementById('myDiv').innerHTML = result;
}
4

1 回答 1

0

首先,你amort用四个参数调用你的函数:

onclick="amort('principalInput','interestInput','termsInput','myDiv')"

但它只定义为接受三个:

function amort(principalInput, interestInput, termsInput)

将第四个参数添加到函数定义中,然后在函数中使用它而不是硬编码输出 div 名称。

接下来,terms在你使用它之前是没有定义的。您需要添加一行,从表单输入中解析它。您需要为HTML 表单value中的标签添加属性,option以便您可以阅读多个术语。<select>有关该语法的外观示例,请参阅 MDN 页面。

principalInput.toFixed(2)principalInput不是数字时,您也会打电话。您之前在代码中从该字段中读取了一个值。我认为您的意思是改用它。

于 2013-06-29T02:24:25.650 回答