2015-05-21 104 views
0

我無法顯示我的貸款計算器在文本字段中的計算,這裏是我的代碼..我希望你能幫助我。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>&nbsp; 
 
    <label id="errorThree"></label> 
 

 

 
    <br> 
 
    <br><span>Mobile Number(Required)</span> 
 
    <br> 
 
    <input type="integer" name="contactNo" id="contactNo" maxlength="11" />&nbsp; 
 
    <label id="errorEight"></label>&nbsp; 
 
    <label id="errorAlphaFour"></label>&nbsp; 
 
    <label id="errorMinThree"></label>&nbsp; 
 
    <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'" />&nbsp; 
 
    <label id="errorNine"></label>&nbsp; 
 
    <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>&nbsp; 
 
    <label id="errorAlphaSeven"></label>&nbsp; 
 
    <label id="errorMinSix"></label>&nbsp; 
 
    <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>&nbsp; 
 
    <label id="errorAlphaEight"></label>&nbsp; 
 
    <label id="errorMinSeven"></label>&nbsp; 
 
    <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>

什麼都沒有發生,當我點擊計算,我真的會並欣賞你的幫助球員

+2

在JavaScript控制檯中的任何錯誤? – Barmar

+0

沒有錯誤mate @Barmar –

+0

您需要在腳本週圍添加「document.ready」,因爲在構建時沒有關於對象值的任何信息......只有在文檔準備就緒後才知道。 – Jobst

回答

2

該pro blem是這樣的:

<input id="calculate" type="button" name="calculate" value="Calculate Loan" onclick="calculate();" /> 

所有id小號成爲全局變量,故得名calculate指的是這個輸入元素,而不是功能。請更改此id或更改函數的名稱。

您還有一個錯字。在JavaScript中,你指的是document.form.amountOfPayement,但是在HTML中它是name="amountOfPayment"。這些需要匹配。

這兩個問題在Javascript控制檯中都會導致錯誤。第一個投訴0123',第二個導致嘗試訪問undefinedvalue屬性的錯誤。我不確定你爲什麼沒有在瀏覽器中看到這些錯誤。

var select = document.getElementById('loantype'); 
 
var input = document.getElementById('interest'); 
 
select.onchange = function() { 
 
    input.value = select.value; 
 
} 
 

 
function calculate_loan() { 
 
    // 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>&nbsp; 
 
    <label id="errorThree"></label> 
 

 

 
    <br> 
 
    <br><span>Mobile Number(Required)</span> 
 
    <br> 
 
    <input type="integer" name="contactNo" id="contactNo" maxlength="11" />&nbsp; 
 
    <label id="errorEight"></label>&nbsp; 
 
    <label id="errorAlphaFour"></label>&nbsp; 
 
    <label id="errorMinThree"></label>&nbsp; 
 
    <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'" />&nbsp; 
 
    <label id="errorNine"></label>&nbsp; 
 
    <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>&nbsp; 
 
    <label id="errorAlphaSeven"></label>&nbsp; 
 
    <label id="errorMinSix"></label>&nbsp; 
 
    <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_loan();"> 
 
     <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_loan();" /> 
 
    <br> 
 
    <br> 
 
    <br><span>Amount Of Payment Per Month (Php)</span> 
 
    <br> 
 
    <input type="integer" name="amountOfPayement" id="amountOfPayement" maxlength="6" onchange="calculate_loan();" disabled="disabled" /> 
 
    <label id="errorFourteen"></label>&nbsp; 
 
    <label id="errorAlphaEight"></label>&nbsp; 
 
    <label id="errorMinSeven"></label>&nbsp; 
 
    <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>

+0

謝謝隊友!你幫了很多忙 –

-3

你需要把這個到表單:

<script> 
var select = document.getElementById('loantype'); 
var input = document.getElementById('interest'); 
select.onchange = function() { 
    input.value = select.value; 
} 
</script> 

的元素後,

+0

他沒有使用jQuery,這是行不通的! – Barmar

+0

對不起,沒有看到改變它。 – Jobst