简体   繁体   English

表单上的 onsubmit = "function( )" 不起作用

[英]onsubmit = "function( )" on form not working

I'm doing form validation, when I call my function 'validtae( )' using 'onsbmit' in the form it doesn't work.我正在进行表单验证,当我在表单中使用“onsbmit”调用我的 function“validtae()”时,它不起作用。 when I click the button submit the validation that supposes to happen doesn't work I have no idea why it's not working, I tried to add return in onsubmit still not working.当我单击按钮提交假设发生的验证不起作用我不知道为什么它不起作用,我试图在 onsubmit 中添加返回仍然不起作用。 I tried calling the function with onfocus inside the input tag and it works but I want to work when I click the button.我尝试在输入标签内使用 onfocus 调用 function 并且它有效但我想在单击按钮时工作。

 // getting inputs const namee = document.forms['myForm']['name']; const email = document.forms['myForm']['email']; const password =document.forms['myForm']['password']; // getting where the erroe gonna show const nameerror = document.querySelector('#nameerror'); const emailerror = document.querySelector('#emailerror'); const passworderror = document.querySelector('#passworderror'); // validation function function validate(){ if(namee.value == "" ){ namee.style.border = "2px solid red"; nameerror.textContent ="name cannot be blank"; namee.focus(); return false; } if(email.value == "" ){ email.style.border = "2px solid red"; emailerror.textContent ="email cannot be blank"; email.focus(); return false; } if(password.value == "" ){ password.style.border = "2px solid red"; passworderror.textContent ="password cannot be blank"; password.focus(); return false; } }
 <form name="myForm" method="post" onsubmit="return validate()"> <input type="text" id="name" name="name" placeholder="Name" required> <div id="nameerror"></div> <input type="email" id="email" name="email" placeholder="Email" required> <div id="emailerror"></div> <input type="password" id="password" name="password" placeholder="Create a password" required> <button id="submit" type="submit">Sign Up</button> </form>

I assume you are wondering why the error message isn't shown.我假设您想知道为什么没有显示错误消息。 That's likely because the browser's own validation for the required attribute kicks in before onsubmit is called.这可能是因为在调用onsubmit之前,浏览器自己required属性的验证开始了。 Ie validate isn't even called at the moment, which you can easily verify by adding a console.log call.即此刻甚至没有调用validate ,您可以通过添加console.log调用轻松验证。

If that's not what you want then remove the required attributes.如果这不是您想要的,则删除required的属性。

 // getting inputs const namee = document.forms['myForm']['name']; const email = document.forms['myForm']['email']; const password =document.forms['myForm']['password']; // getting where the erroe gonna show const nameerror = document.querySelector('#nameerror'); const emailerror = document.querySelector('#emailerror'); const passworderror = document.querySelector('#passworderror'); // validation function function validate(){ if(namee.value == "" ){ namee.style.border = "2px solid red"; nameerror.textContent ="name cannot be blank"; namee.focus(); return false; } if(email.value == "" ){ email.style.border = "2px solid red"; emailerror.textContent ="email cannot be blank"; email.focus(); return false; } if(password.value == "" ){ password.style.border = "2px solid red"; passworderror.textContent ="password cannot be blank"; password.focus(); return false; } }
 <form name="myForm" method="post" onsubmit="return validate()"> <input type="text" id="name" name="name" placeholder="Name"> <div id="nameerror"></div> <input type="email" id="email" name="email" placeholder="Email"> <div id="emailerror"></div> <input type="password" id="password" name="password" placeholder="Create a password" > <button id="submit" type="submit">Sign Up</button> </form>

Alternatively just let the browser handle the validation and remove your own validate function.或者让浏览器处理验证并删除您自己的validate function。

If any of the validation checks failed you should use event.preventDefault()如果任何验证检查失败,您应该使用event.preventDefault()

function validate(event){
    let invalid = 0
    if (namee.value == "" ) {
        namee.style.border = "2px solid red";
        nameerror.textContent ="name cannot be blank";
        namee.focus();
        invalid++;
    }
    if (email.value == "" ){
        email.style.border = "2px solid red";
        emailerror.textContent ="email cannot be blank";
        email.focus();
        invalid++;
    }
    if (password.value == "" ){
        password.style.border = "2px solid red";
        passworderror.textContent ="password cannot be blank";
        password.focus();
        invalid++;
    }
    if (invalid > 0) {
        event.preventDefault()
        return false
    }
}
<input type="submit" value="submit"/>

Change the submit button to input, it will work.将提交按钮更改为输入,它将起作用。

Check this out 看一下这个
 <input type="text" id="name" name="name" placeholder="Name" required> <div id="nameerror"></div> <input type="email" id="email" name="email" placeholder="Email" required> <div id="emailerror"></div> <input type="password" id="password" name="password" placeholder="Create a password" required> <button onsubmit="validate()" id="submit" type="submit">Sign Up</button>

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

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