这是我第一周自学javascript而且遇到了问题。
我正在尝试创建一个简单的抵押贷款'贷款价值'计算器。我的计算器工作正常,但我正在尝试为我的两个表单字段中的一个设置最小值验证。
这是指向页面的链接
我的代码在
下面function calculate (form) {
var propVal = form.propertyValue.value; var loanAmt =
form.mortgageAmount.value; var type = form.mortgageType.value;
var ltv = loanAmt / propVal * 100 ;
var roundedLtv = Math.floor(ltv);
if ( type === "First Time Buyer" && roundedLtv <= 91 ) {
document.getElementById("result").innerHTML= ("Your loan to value is: <b>" + (roundedLtv) + "%</b> <br /> We can help place your mortgage");
}
else if ( type === "Moving Home" && roundedLtv <= 91 ) {
document.getElementById("result").innerHTML= ("Your loan to value is: <b>" + (roundedLtv) + "%</b> <br /> We can help place your mortgage");
}
else if ( type === "Remortgage" && roundedLtv <= 86 ) {
document.getElementById("result").innerHTML= ("Your loan to value is: <b>" + (roundedLtv) + "%</b> <br /> We can help place your mortgage");
}
else if ( type === "Buy To Let Purchase" && roundedLtv <= 81 ) {
document.getElementById("result").innerHTML= ("Your loan to value is: <b>" + (roundedLtv) + "%</b> <br /> We can help place your mortgage");
}
else if ( type === "Buy To Let Remortgage" && roundedLtv <= 76 ) {
document.getElementById("result").innerHTML= ("Your loan to value is: <b>" + (roundedLtv) + "%</b> <br /> We can help place your mortgage");
}
else
{
document.getElementById("result").innerHTML= ("Your loan to value is: <b>" + (roundedLtv) + "%</b> <br /> Unfortunatly we cannot help you place a mortgage");
}
}
所有工作正常,直到这一点,我试图提醒用户贷款金额需要大于45000.在HTML下面你会看到我正在尝试使用onBlur函数
function loancheck (propVal,loanAmt,form) {
if (loanAmt >= 45000) {
document.getElementById("val1").innerHTML= ("Thank You");
}
else
{
document.getElementById("val1").innerHTML= ("Higher");
}
}
这是随身携带的HTML
<form name="myform" ACTION="" METHOD="GET"> Mortgage Type
<select name="mortgageType">
<option>First Time Buyer</option>
<option>Moving Home</option>
<option>Remortgage</option>
<option>Buy To Let Purchase</option>
<option>Buy To Let Remortgage</option>
</select>
<br />
Value <input name="propertyValue" type="text" value=""/><br />
Mortgage Amount <input name="mortgageAmount" type="text" value="" onBlur="loancheck(this.value)" />
<p id="val1"></p>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="calculate(this.form)">
</form>
<br /><br />
<p id="result"> </p>
我确定我做的事情本质上是错误的,但如果有人能指出我,我会非常感激。
答案 0 :(得分:1)
调用loancheck
时,变量loanAmt
未定义,因为没有足够的参数传递给函数。考虑以下列方式清理功能定义:
function loancheck (loanAmt) { // remove unused parameters
var userAlert = document.getElementById("val1") // don't repeat yourself
if (loanAmt >= 45000) {
userAlert.innerHTML= "Thank You"; // remove extraneous parens
} else {
userAlert.innerHTML= "Higher";
}
}
答案 1 :(得分:0)
当你致电loancheck
时,你会这样做:
loancheck(this.value)
但是,loancheck
有三个参数:
function loancheck (propVal,loanAmt,form)
由于loanAmt
是calculate
函数中的私有变量,因此您需要先调用calculate然后返回loanAmt
。或者,使loanAmt
成为全局变量,并在用户更改相应字段时更新它。
答案 2 :(得分:0)
你没说实际发生了什么:) 但是我看到你的loancheck函数需要3个变量。你的onblur处理程序传递一个变量但你的函数期望第二个变量“loanAmt”在那里。有意义吗?
答案 3 :(得分:0)
您正在onBlur中调用loancheck(this.value)
但是你的函数定义有loancheck(propVal,loanAmt,form)。
您传递的是一个获取副本到proVal的值。
loadnAmt,表单未定义。