2013-06-21 98 views
0

我已經得到了一個計算器的腳本,只要點擊或更改字段就可以計算表單。它工作的很好,但我也希望在頁面加載時使用默認值計算表單。所以我加了爲什麼不這個window.onload觸發我的功能?

window.onload = Calc(form);

在窗體的末尾,但它不起作用,窗體不計算。我究竟做錯了什麼?這可能是一個明顯的答案,對我而言並不明顯。謝謝你的幫助!

<script type="text/javascript">// <![CDATA[ 
// Javascript Mortgage Calculator 
function Calc(form) { 
var x, y, incm, intRate = 0; 
    intRate = form.rate.value/100/12; 
    p = form.term.value * 12; 
    form.down.value = form.price.value * form.pdown.value/100; 
    form.loan.value = form.price.value - form.down.value; 
    x = form.loan.value * intRate * Math.pow(1 + intRate, p); 
    y = Math.pow(1 + intRate, p) - 1; 
    form.pni.value = parseInt(x/y); 
    form.taxes.value = parseInt(form.price.value * form.taxrate.value/1200); 
    form.ins.value = parseInt(form.price.value * form.insrate.value/1200); 
       form.pmirate.value = 0; 
    if (form.pdown.value < 20) { 
    form.pmirate.value = .5; 
    } 
    if (form.pdown.value < 10) { 
    form.pmirate.value = .9; 
    } 
    form.pmi.value = parseInt(form.loan.value * form.pmirate.value/1200); 
    form.piti.value = parseInt(form.pni.value) + parseInt(form.taxes.value) + parseInt(form.ins.value) + parseInt(form.pmi.value); 



} 
function Hnt(form,f) { 
var text = "No Hint available for this field!"; 
    if (form.pni.value == "") { 
    Calc(form); 
    } 
    if (f == "price") { 
    text = "Enter the purchase price of the home."; 
    } 
    if (f == "pdown") { 
    text = "Enter the percent that you will put down, usually 5, 10, or 20%."; 
    } 
    if (f == "loan") { 
    text = "This is the financed loan amount borrowed from the lender."; 
    } 
    if (f == "down") { 
    text = "This is the down payment amount, not including closing costs."; 
    } 
    if (f == "rate") { 
    text = "Enter the interest rate for the loan in percent."; 
    } 
    if (f == "term") { 
    text = "Enter the term in years for the loan, usually 15 or 30."; 
    } 
    if (f == "pni") { 
    text = "This is the total principal and interest portion of your monthly payment."; 
    } 
    if (f == "taxrate") { 
    text = "This is the total percentage per year of state, county, and city taxes, as a percentage of the purchase price."; 
    } 
    if (f == "insrate") { 
    text = "This is Homeowner' Insurance rate per year, as a percentage of the loan amount."; 
    } 
    if (f == "ins") { 
    text = "This is the total Homeowners Insurance per month."; 
    } 
    if (f == "pmirate") { 
    text = "Private Mortgage Insurance is required for loans over 80%. This is calculated as a percentage per year of the loan amount."; 
    } 
    if (f == "pmi") { 
    text = "This is the Private Mortgage Insurance per month."; 
    } 
    if (f == "piti") { 
    text = "This is the total monthly payment, PITI, Principal + Interest + Taxes + Insurance"; 
    } 
    if (f == "inc") { 
    text = "Enter your total combined annual gross income."; 
    } 
    if (f == "taxes") { 
    text = "This is the total taxes per month."; 
    } 
    if (f == "debt") { 
    text = "Enter your total monthly debt, car loans, credit cards, student loans, etc."; 
    } 
    if (f == "taxes") { 
    text = "This is the total taxes per month."; 
    } 
    if (f == "fr") { 
    text = "Ratio of PITI to your monthly income. Most lenders require 28% or less."; 
    } 
    if (f == "bk") { 
    text = "Ratio of (PITI+debt) to your monthly income. Most lenders require 36% or less"; 
    } 
    if (f == "qualtext") { 
    text = "Do you qualify?, and if you don't, why not?"; 
    } 
    if (f == "hint") { 
    text = "This field displays hints for the field that currently has the cursor in it."; 
    } 

} 
// ]]></script> 
<center><form></form></center><center> 
<p></p> 
<form id="Calculator"> 
<table cellspacing="0" bordercolordark="#999999" cellpadding="0" bordercolorlight="#CCCCCC" border="2" style="background-color: #fffdf6;"> 
<tbody> 
<tr><th colspan="3"> 
<h3>Mortgage Calculator</h3> 
<p>Click any field to calculate the payment information.</p> 
</th></tr> 
<tr> 
<td> 
<table valign="top"> 
<tbody> 
<tr><th valign="top" align="center" colspan="2">Purchase Information</th></tr> 
<tr> 
<td align="right">Home Price $</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" value="200000" name="price" type="text" /></td> 
</tr> 
<tr> 
<td align="right">Down %<input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="1" value="10" name="pdown" type="text" />$</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="down" type="text" /></td> 
</tr> 
<tr> 
<td align="right">Loan Amount $</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="loan" type="text" /></td> 
</tr> 
<tr> 
<td align="right">Interest Rate %</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" value="5" name="rate" type="text" /></td> 
</tr> 
<tr> 
<td align="right">Term Years</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" value="30" name="term" type="text" /></td> 
</tr> 
</tbody> 
</table> 
</td> 
<td> 
<table valign="top"> 
<tbody> 
<tr><th valign="top" align="center" colspan="3">Payment Information</th></tr> 
<tr> 
<td align="right" colspan="2">Principal and Interest $</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="pni" type="text" /></td> 
</tr> 
<tr> 
<td style="visibility: hidden;" align="right">Prop Tax Rate %</td> 
<td style="visibility: hidden;"><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="4" value="0" name="taxrate" type="text" />= $</td> 
<td style="visibility: hidden;"><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="taxes" type="text" /></td> 
</tr> 
<tr> 
<td align="right" style="visibility: hidden;">Insurance Rate %</td> 
<td style="visibility: hidden;"><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="4" value="0" name="insrate" type="text" />= $</td> 
<td style="visibility: hidden;"><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="ins" type="text" /></td> 
</tr> 
<tr> 
<td align="right">PMI Rate %</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="4" name="pmirate" type="text" />= $</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="pmi" type="text" /></td> 
</tr> 
<tr> 
<td align="right" colspan="2">Total Payment $</td> 
<td><input onfocus="Hnt(this.form,this.name)" onchange="Calc(this.form)" size="5" name="piti" type="text" /></td> 
</tr> 
<tr></tr> 
</tbody> 
</table> 
</td> 
<td></td> 
</tr> 
</tbody> 
</table> 
</form></center> 
<script type="text/javascript">// <![CDATA[ 
window.onload=Calc(form) ; 
// ]]></script> 

回答

4

您尚未註冊功能,但此功能的返回值爲undefined

更換

window.onload=Calc(form) ; 

window.onload=function(){ // define a new function 
    Calc(document.getElementById('Calculator')) // find the form and pass it as argument 
}; 
+0

這是有道理的,它完美的作品!非常感謝解決方案和解釋! –

1

這是因爲你正在採取Calc的結果並讓onload調用它。也許你應該把它包裝在另一個函數中。

相關問題