我無法顯示我的貸款計算器在文本字段中的計算,這裏是我的代碼..我希望你能幫助我。javascript貸款計算器按鈕計算點擊時沒有任何反應
var select = document.getElementById('loantype');
var input = document.getElementById('interest');
select.onchange = function() {
input.value = select.value;
}
function calculate() {
// Get the user's input from the form. Assume it is all valid.
// Convert interest from a percentage to a decimal, and convert from
// an annual rate to a monthly rate. Convert amountOfPayement period in years
// to the number of monthly payments.
var amountBorrowed = document.form.amountBorrowed.value;
var interest = document.form.interest.value/100/12;
var payments = document.form.durationOfPayment.value * 12;
// Now compute the monthly amountOfPayement figure, using esoteric math.
var x = Math.pow(1 + interest, payments);
var monthly = (amountBorrowed * x * interest)/(x - 1);
// Check that the result is a finite number. If so, display the results
if (!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY)) {
document.form.amountOfPayement.value = round(monthly);
document.form.total.value = round(monthly * payments);
document.form.totalinterest.value =
round((monthly * payments) - amountBorrowed);
}
// Otherwise, the user's input was probably invalid, so don't
// display anything.
else {
document.form.amountOfPayement.value = "";
document.form.total.value = "";
document.form.totalinterest.value = "";
}
}
// This simple method rounds a number to two decimal places.
function round(x) {
return Math.round(x * 100)/100;
}
<form name="form" action="action.php" method="post" onsubmit="return validate()">
<div class="wrapper">
<span>Select Loan Type</span>
<br>
<select type="text" id="loantype" name="loantype">
<option value="walanglaman" selected="selected" disabled>LOAN TYPE:</option>
<option value="6">BUSINESS LOAN</option>
<option value="3">PERSONAL LOAN</option>
<option value="4">SEAMAN LOAN</option>
<option value="4">COLLATERALIZED LOAN</option>
<option value="3">REGULAR LOAN LOAN</option>
<option value="5">CHECK REDISCOUNTING</option>
<option value="4">LOAN AGAINST DEPOSIT</option>
</select>
<label id="errorThree"></label>
<br>
<br><span>Mobile Number(Required)</span>
<br>
<input type="integer" name="contactNo" id="contactNo" maxlength="11" />
<label id="errorEight"></label>
<label id="errorAlphaFour"></label>
<label id="errorMinThree"></label>
<label id="invalidFormat"></label>
<br>
<br><span>Email Address(Optional)</span>
<br>
<input type="text" name="email" id="email" placeholder="Email Address" onfocus="this.placeholder=''" onblur="this.placeholder='Email Address'" />
<label id="errorNine"></label>
<label id="errorAlphaFive"></label>
<br>
<br>
<span>Reason of Loan</span>
<br>
<select type="text" id="reasonOfLoan" name="reasonOfLoan">
<option value="empty4" selected="selected" disabled>Select One</option>
<option value="For Emergency">For Emergency</option>
<option value="For Business">For Business</option>
</select>
<label id="errorTwelve"></label>
<br>
</div>
<div class="contentsTwo">
<br><span>Amount Borrowed(Php)</span>
<br>
<input type="integer" name="amountBorrowed" id="amountBorrowed" maxlength="7" placeholder="Amount Borrowed" onfocus="this.placeholder=''" onblur="this.placeholder='Amount Borrowed'" />
<label id="errorThirteen"></label>
<label id="errorAlphaSeven"></label>
<label id="errorMinSix"></label>
<label id="errorInvalidFormatTwo"></label>
<br>
<br><span>Interest (%)</span>
<br>
<input type="integer" name="interest" id="interest" maxlength="6" disabled="disabled" onchange="checkPrice()" />
<br>
<span>Duration of Payment</span>
<br>
<select type="text" id="durationOfPayment" name="durationOfPayment" onchange="calculate();">
<option value="empty5" selected="selected" disabled>Select One</option>
<option value="3">Three Months</option>
<option value="6">Six Months</option>
<option value="12">One Year</option>
<option value="18">One 1/2 Year</option>
</select>
<label id="errorFifteen"></label>
<br>
<br>
<input id="calculate" type="button" name="calculate" value="Calculate Loan" onclick="calculate();" />
<br>
<br>
<br><span>Amount Of Payment Per Month (Php)</span>
<br>
<input type="integer" name="amountOfPayment" id="amountOfPayment" maxlength="6" onchange="calculate();" disabled="disabled" />
<label id="errorFourteen"></label>
<label id="errorAlphaEight"></label>
<label id="errorMinSeven"></label>
<label id="errorInvalidFormatThree"></label>
<br>
<br><span>Total Interest</span>
<br>
<input type="integer" name="totalinterest" id="totalinterest" maxlength="6" disabled="disabled" onchange="checkPrice()" />
<br>
<br><span>Total</span>
<br>
<input type="integer" name="total" id="total" maxlength="6" disabled="disabled" />
<br>
<br>
<label id="errorOther"></label>
<br>
</div>
<div class="btn">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<input id="submit" type="submit" name="submit" value="Submit" />
<br>
<br>
</div>
</form>
什麼都沒有發生,當我點擊計算,我真的會並欣賞你的幫助球員
在JavaScript控制檯中的任何錯誤? – Barmar
沒有錯誤mate @Barmar –
您需要在腳本週圍添加「document.ready」,因爲在構建時沒有關於對象值的任何信息......只有在文檔準備就緒後才知道。 – Jobst