简体   繁体   English

如何使用DOM和Javascript调用多个函数

[英]How to call multiple functions with DOM and Javascript

I'm working on an assignment and need to validate multiple inputs. 我正在分配作业,需要验证多个输入。 I have created multiple functions and am having trouble calling each one. 我创建了多个函数,在调用每个函数时遇到麻烦。 The first one is the only one that will call. 第一个是唯一会调用的。 The other two will just hang and do nothing. 其他两个只会挂起而无所事事。

If I do a single function call for oninput at the form tag it works. 如果我在表单标签上对oninput进行单个函数调用,那么它将起作用。 Just that it automatically calls the function and all validations. 只是它会自动调用函数和所有验证。 This causes all the prompts to come out at the same time which I don't want. 这会导致所有提示都在我不想同时出现。 This is why the oninput call is being done at the input tag. 这就是为什么在输入标签上进行oninput调用的原因。

HTML: HTML:

    <div id="nameValidate"></div>
    <label for="name">Name:</label>
    <input type="text" id="nameID" 
        oninput="nameValidation()"/> <br />

    <div id="emailValidate"></div>
    <label for="email">Email:</label>
    <input type="text" id="emailID" 
        oninput="emailValidation()"/> <br />

    <div id="phoneValidate"></div>
    <label for="phone">Phone Number:</label>
    <input type="number" id="phoneID" 
        oninput="phoneValidation()"/>

Javascript Java脚本

    function nameValidation() {
        var name = document.getElementById("nameID").value;

        if (name.length < 3) {
            document.getElementById("nameValidate").innerText = "Please 
                enter your full name.";
        }
        else if (name.length > 3) {
            document.getElementById("nameValidate").innerText = "";
        }
    }

    function emailValidation() {
        var email = document.getElementById("emailID").value;

        if (!email.match(".com") && email < 5) {
            document.getElementById("emailValidate").innerText = "Please 
                enter your full email address.";
        }
        else {
            document.getElementById("emailValidate").innerText = "";
        }
    }

    function phoneValidation() {
        var phone = document.getelementbyid("phoneID").value;

        if (phone == "" || phone.length < 10) {
            document.getelementbyid("phoneValidate").innertext = "please 
                enter your full phone number.";
        }
        else if () {
            document.getelementbyid("phoneValidate").innertext = "";
        }
    }

Firstly , your elseif has brackets but the condition is empty. 首先 ,您的elseif括号,但条件为空。 Check your console, it should be showing a syntax error because: 检查您的控制台,它应该显示语法错误,因为:

} else if () {
    document.getelementbyid("phoneValidate").innertext = "";
}

is not valid syntax. 是无效的语法。 Turn it into an else . 把它变成else

Secondly , the function: 其次 ,功能:

document.getelementbyid("phoneValidate").innertext = "";

does not exist on document , however, getElementById does. document上不存在,但是getElementById确实存在。

Finally , ensure that you use the console to help you debug your code. 最后 ,请确保使用控制台来帮助您调试代码。

Let's back up a minute and break some very bad habits that someone who doesn't know any better is teaching you. 让我们备份一分钟,摆脱一些非常不好的习惯,那些不了解的人正在教你。

Do not set up events using inline HTML event attributes (ie. onclick ). 不要使用内联HTML事件属性(即onclick )设置事件。 This is a 25+ year old technique that persists today because people just copy/paste it and it seems to work in many cases. 这是一种已有25年历史的技术,一直存在到今天,因为人们只是复制/粘贴它,并且在许多情况下似乎都可以使用。 However, there are a number of very good reasons not to use this ancient technique that just will not die. 但是,有很多很好的理由不使用这种永不过时的古老技术 Separate your JavaScript from your HTML and use modern, standards-based approaches to event handling with .addEventListener() . 将您的JavaScript与HTML分开,并使用基于现代标准的方法通过.addEventListener()进行事件处理。

You've also mis-capitalized .getElementById() when you were getting the phone data and this would cause an error in your code that would prevent it from continuing. 在获取电话数据时,还会误用.getElementById()大写,这将导致代码中的错误,从而阻止其继续。 Always work with your developer tools (F12) open and the Console tab showing as this is where error messages will appear. 始终在打开开发人员工具(F12)的情况下工作,并显示“控制台”选项卡,因为这将显示错误消息。

Next, only query the DOM once for elements that you'll need over and over. 接下来,只查询DOM一次又一次需要的元素。 This means remove all the document.getElementById() lines from inside the functions and move them so they just get executed only once. 这意味着从函数内部删除所有document.getElementById()行,并移动它们,以便仅执行一次。

And, don't make references to properties of DOM elements, make the references to the element itself. 并且,不要引用DOM元素的属性,而要引用元素本身。 This way, you scan the document just once to get the element reference, but then you can get any property you like when you need it without having to scan the document for the same element again. 这样,您只扫描文档一次即可获得元素引用,但是随后您可以在需要时获取所需的任何属性,而无需再次扫描文档中的同一元素。

Next, don't use .innerText as it is non-standard. 接下来,不要使用.innerText因为它是非标准的。 Use .textContent instead. 请改用.textContent

And, don't use self-terminating tag syntax (ie. <br /> , <input /> ). 并且,请勿使用自终止标记语法(即<br /><input /> )。 Here's why. 这就是为什么。

So, here's what your code should look like: 因此,您的代码应如下所示:

 // Get references to the elements you'll be working with just once var userName = document.getElementById("nameID"); var nameValidate = document.getElementById("nameValidate"); var email = document.getElementById("emailID"); var emailValidate = document.getElementById("emailValidate"); var phone = document.getElementById("phoneID"); var phoneValidate = document.getElementById("phoneValidate"); // Set up your event handlers in JavaScript, not HTML userName.addEventListener("input", nameValidation); email.addEventListener("input", emailValidation); phone.addEventListener("input", phoneValidation); function nameValidation() { if (this.value.length < 3) { nameValidate.textContent = "Please enter your full name."; } else { nameValidate.textContent = ""; } } function emailValidation() { // Check the last 4 characters of the input if ((this.value.substr(this.value.length - 4) !== ".com") && email.value.length < 5) { emailValidate.textContent = "Please enter your full email address."; } else { emailValidate.textContent = ""; } } function phoneValidation() { if (phone.value == "" || phone.value.length < 10) { phoneValidate.textContent = "please enter your full phone number."; } else { phoneValidate.textContent = ""; } } 
 <div id="nameValidate"></div> <label for="name">Name:</label> <input type="text" id="nameID"> <br> <div id="emailValidate"></div> <label for="email">Email:</label> <input type="text" id="emailID"> <br> <div id="phoneValidate"></div> <label for="phone">Phone Number:</label> <input type="number" id="phoneID"> 

Finally, as a professional technology trainer for over 25+ years, I would strongly advise you to inform whoever is teaching you these outdated techniques that they are doing you and anyone else they are teaching a disservice. 最后,作为超过25年以上的专业技术培训师,我强烈建议您告知正在向您教授这些过时技术的人正在为您提供帮助,以及正在为其他人提供帮助的其他人。 Modern web development is hard-enough without having to unlearn bad habits brought on by those who don't know any better. 现代化的Web开发是足够硬的,而不必学习那些不了解的人所带来的不良习惯。

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

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