简单的 Javascript Onblur If else 函数
这是我自学 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>
我确信我正在做一些根本错误的事情,但如果有人可以指出我将非常感激。
This is my first week teaching myself javascript and i have run into a problem.
I am trying to create a simple mortgage 'loan to value' calculator. My calculator works fine but I am trying to set up a minimum value validation for one of my two form fields.
Here is a link to the page
My code is below
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");
}
}
All works ok until this point, where i am trying to alert the user that the loan amount needs to be larger than 45000. Below in the HTML you will see im am trying to use the onBlur function
function loancheck (propVal,loanAmt,form) {
if (loanAmt >= 45000) {
document.getElementById("val1").innerHTML= ("Thank You");
}
else
{
document.getElementById("val1").innerHTML= ("Higher");
}
}
This is the html to go with it
<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>
Im sure I'm doing something fundamentally wrong but if someone could point it out to me i would be most thankful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当调用
loancheck
时,变量loanAmt
未定义,因为没有足够的参数传递给函数。考虑通过以下方式清理函数定义:When
loancheck
is called, the variableloanAmt
is undefined, because not enough parameters were passed to the function. Consider cleaning up the function definition in the following way:当您调用
loancheck
时,您会执行以下操作:但是,
loancheck
需要三个参数:由于
loanAmt
是calculate 函数,您需要先调用calculate,然后返回
loanAmt
。或者,将 loanAmt 设为全局变量,并在用户更改相应字段时更新它。When you call
loancheck
you do this:However,
loancheck
takes three parameters:Since
loanAmt
is a private variable within thecalculate
function, you'll need to call calculate first and then return theloanAmt
. Or, makeloanAmt
a global variable and update it whenever the user changes the appropriate field.你没有说实际发生了什么:)
但我发现你的贷款检查函数需要 3 个变量。您的 onblur 处理程序传递一个变量,但您的函数期望第二个变量“loanAmt”在那里。有道理吗?
You didn't say what actually happens :)
But I see that your loancheck function takes 3 variables. Your onblur handler passes one variable but your function is expecting the second variable "loanAmt" to be there. Makes sense?
您在onBlur 中调用loancheck(this.value)
但您的函数定义有loancheck (propVal,loanAmt,form)。
您正在传递一个值,该值会复制到 proVal。
loadnAmt、form 未定义。
You are calling loancheck(this.value) in onBlur
But your function definition has loancheck (propVal,loanAmt,form).
You are passing one value which get copies to proVal.
loadnAmt, form are undefined.