贷款计算器与下拉框使用JavaScript

时间:2016-04-11 19:26:45

标签: javascript jquery function calculator

大家好,我正试图用下拉框制作一个贷款计算器。您输入您想借入的金额,然后出现一个下拉利率,利率为6.75%或9.71%。然后输入您想要偿还贷款的月数。但是我已经做了我能想到的一切,而且它没有工作?谁能帮帮我吗?感谢

这是html

<body> 
<p>Loan Amount: €<input id="amount" type="number" min="1" max="1000000" onchange="computeLoan()"></p>


<p>Interest Rate:
 <select>
    <option value="" disabled="disabled" selected="selected">Select A Loan Rate</option>
    <!-- 6.75 loan -->
    <option value="6.75" input id="interest_rate" onchange="computeLoan()">6.75%</option>
    <!-- 9.71 loan -->
    <option value="9.71" input id="interestRate" onchange="computeLoan()">9.75%</option>
</select>


<p>Months: <input id="months" type="number" min="1" max="60" value="1" step="1" onchange="computeLoan()"></p>


<h2 id="payment"></h2>

这是javascript

// first function excute at a rate of 6.75% APR
function computeLoan()
{
    var amount = document.getElementById('amount').value;

    var interest_rate = document.getElementById('interest_rate').value;

    var months = document.getElementById('months').value;

    var interest = (amount * (interest_rate * .01)) / months;

    var payment = ((amount / months) + interest).toFixed(2);

    payment = payment.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

    document.getElementById('payment').innerHTML = "Your Monthly Payment = € "+payment;

}

// Second function excute at a rate of 9.71% APR

function computeLoan(){
    var amount = document.getElementById('amount').value;

    var interestRate = document.getElementById('interestRate').value;

    var months = document.getElementById('months').value;

    var interest = (amount * (interestRate * .01)) / months;

    var payment = ((amount / months) + interest).toFixed(2);

    payment = payment.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

    document.getElementById('payment').innerHTML = "Monthly Payment = $"+payment;
}

如果有人能帮助我做什么就会很棒!谢谢

1 个答案:

答案 0 :(得分:0)

您不能拥有两个共享相同名称的功能。

使用条件逻辑创建一个函数,该函数取决于select的值。我希望有所帮助!

此外,您应该将onchange属性移动到select元素而不是option元素上,并获取select的值。