简体   繁体   English

我如何调用HTML DOM事件对象(例如:onclick,onblur等)的嵌套函数?

[英]How can I call to HTML DOM Event Object (for example: onclick, onblur, etc..) nested functions?

Good day! 美好的一天! Let's assume that I have a group of textfields in my page, each of the textfield has its own validation during onblur, for example the amount textfield, If I remove the focus on it, it will check if it is greater than 100,000. 假设我的页面中有一组文本字段,每个文本字段在onblur期间都有自己的验证,例如,数量文本字段。如果我删除焦点,它将检查它是否大于100,000。

<input type = "text" id = "amount" placeholder="Amount" onblur="validateAmount()">

function validateAmount(){
  var amount = document.getElementById("amount").value;
  if (amount > 100000){
      document.getElementById("errormessage").innerHTML = "Invalid! Enter a number less than 100,000!"; //Let's just assume that I have a label for the error message
  }
}

Oops! 糟糕! This is just one of the textfields that has a validation, what If I have 5, let's assume that I click the button to process data with 3 problems on my validation, so the data will not be processed because of those errors. 这只是具有验证的文本字段之一,如果我有5个,我们假设单击按钮以处理验证中存在3个问题的数据,因此由于这些错误,将不会处理数据。 In my case I'm going to use onclick event, take a look of this example: 在我的情况下,我将使用onclick事件,请看以下示例:

<button onclick = "checkData()"> Apply </button>

function checkData(){
//Here in checkData() method/function, I want to put the validateAmount() function above and other functions of validation during onblur to count and state the number of errors before submitting the data. 
    var numberOfErrors = 0;
    function validateAmount(){
      var amount = document.getElementById("amount").value;
      if (amount > 100000){
          document.getElementById("errormessage").innerHTML = "Invalid! Enter a number less than 100,000!"; //Let's just assume that I have a label for the error message
          numberOfErrors++;
      }
    }
    /* And more validating functions here
    */

    // After validating all the textfields.
    if(numberOfErrors == 0){
    alert("Congrats! You already submitted the loan");
    } else {
    alert("Error! Check your inputs!");
    }
}

So my question is how can I call the function inside the function on HTML DOM events? 所以我的问题是,如何在HTML DOM事件的函数内部调用函数? Is there any possible and easier way to do this? 有没有可能更简单的方法来做到这一点? Thank you in advance! 先感谢您!

There are many approaches to this, but I recommend using a validation library. 有很多解决方法,但是我建议使用验证库。 Rolling your own is a very risky proposition, and will lead to unmanageable "spaghetti code". 自己动手是一个非常冒险的提议,并且会导致难以管理的“意大利面条代码”。

Here are a couple of libraries you can look into. 您可以查看以下两个库。

http://jqueryvalidation.org http://jqueryvalidation.org

https://angularjs.org https://angularjs.org

You can pass "this" to the event handler, so that it knows which field it's supposed to validate. 您可以将“ this”传递给事件处理程序,以便它知道应该验证哪个字段。 I'd run all the validation functions again when submitting the form instead of keeping a count, because that can get messy if people correct errors, or add errors to previously correct fields. 提交表单时,我会再次运行所有验证功能,而不是保留一个计数,因为如果人们更正错误或将错误添加到先前更正的字段中,这可能会变得混乱。
I added a dummy validator as an example. 我添加了一个虚拟验证器作为示例。

 function checkData(field){ if (!field){ var numberOfErrors = 0; if (!validateAmount()) numberOfErrors++; if (!validateOther()) numberOfErrors++; if (!numberOfErrors){ alert("Congrats! You already submitted the loan"); } else { alert("Error! Check your inputs!"); } } else if (field.id == "amount") validateAmount() else if (field.id == "other") validateOther(); function validateAmount(){ var amount = parseFloat(document.getElementById("amount").value); if (isNaN(amount) || amount > 100000){ document.getElementById("errormessage").innerHTML = "Invalid! Amount must be less than 100,000!"; return(false); } return(true); } function validateOther(){ var other = parseFloat(document.getElementById("other").value); if (isNaN(other) || other > 100){ document.getElementById("errormessage").innerHTML = "Invalid! Other must be less than 100!"; return(false); } return(true); } } 
 <form> <input type = "text" id = "amount" placeholder="Amount" onblur="checkData(this);"> <input type = "text" id = "other" placeholder="Other" onblur="checkData(this);"> <button onclick = "checkData()"> Apply </button> </form> <div id="errormessage"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM