簡單的程序將計算週期性利率,然而該函數是在按鈕中定義和調用的,但仍未顯示的未捕獲參考值仍然存在錯誤。很多的幫助表示讚賞未捕獲的參考誤差函數未定義Javascript
function calculatePayment(){
var loanAmount = document.getElementById("txtLoanAmount").value;
var interestRate = document.getElementById("txtInterestRate").value;
var amortiPeriod = document.getElementById("txtYearlyPeriod").value;
var totalPayment;
var numOfMonths;
//Convert strings from text box to integers
loanAmount = parseInt(loanAmount);
interestRate = parseInt(interestRate);
amortiPeriod = parseInt(amortiPeriod);
//Validate the input boxes to ensure values were added and follow application rules
if (loanAmount == null || loanAmount == ""){
//alert("You must enter a loan amount");
message = "You must enter a loan Amount";
document.getElementById("errorMessage").innerHTML = message;
}else if (interestRate == null || interestRate == ""){
//alert("You must enter a interest rate");
message = "You must enter an interest rate";
document.getElementById("errorMessage").innerHTML = message;
}else if (amortiPerid == null || amortiPeriod == ""){
//alert("You must enter an interest rate");
message = "You must enter an interest rate";
document.getElementById("errorMessage").innerHTML = message;
}else if isNaN(loanAmount || interestRate || amortiPeriod){
message = "The values you enter must be numeric";
document.getElementById("errorMessage").innerHTML = message;
}else{
numOfMonths = amortiPeriod * 12;
paymentRate = loanAmount * interestRate/100;
totalPayment = paymentRate * numOfMonths;
message = totalPayment;
document.getElementById("errorMessage").innerHTML = message;
}
}
下面是HTML:
<tr>
<td><input type="button" onClick="calculatePayment()" name="btnCalculate" value="Calculate Payment"/></td><td><input type="button" name="btnClear" value="Clear"/></td>
</tr>
請添加您的HTML部分 –
*其中*是定義的函數嗎?如果它在全局範圍內不可用,那麼DOM不能在'onclick'中運行它。你可以在函數def後添加:'window.calculatePayment = calculatePayment'。 –
我發現錯誤,就像'}否則,如果isNaN(...)其中括號完全缺失 – adeneo