简单的 Javascript Onblur If else 函数

发布于 2024-12-28 10:45:34 字数 3218 浏览 4 评论 0原文

这是我自学 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

Link

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狂之美人 2025-01-04 10:45:34

当调用 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";
    }
}

When loancheck is called, the variable loanAmt is undefined, because not enough parameters were passed to the function. Consider cleaning up the function definition in the following way:

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";
    }
}
思念满溢 2025-01-04 10:45:34

当您调用 loancheck 时,您会执行以下操作:

loancheck(this.value)

但是,loancheck 需要三个参数:

function loancheck (propVal,loanAmt,form) 

由于 loanAmtcalculate 函数,您需要先调用calculate,然后返回loanAmt。或者,将 loanAmt 设为全局变量,并在用户更改相应字段时更新它。

When you call loancheck you do this:

loancheck(this.value)

However, loancheck takes three parameters:

function loancheck (propVal,loanAmt,form) 

Since loanAmt is a private variable within the calculate function, you'll need to call calculate first and then return the loanAmt. Or, make loanAmt a global variable and update it whenever the user changes the appropriate field.

樱&纷飞 2025-01-04 10:45:34

你没有说实际发生了什么:)
但我发现你的贷款检查函数需要 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?

晨曦÷微暖 2025-01-04 10:45:34

您在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文