简体   繁体   English

填写所有必填字段时如何防止默认提交

[英]How to prevent default submission when ALL required fields are filled in

<div class="container">
    <form action="">
        <fieldset id="contactform">
            <legend>Contact Information</legend>
            <div id="name">
                <div class="info">
                    <label for="name">Name*<br><br></label>
                    <input type="text"  required>
                </div>
            </div>
            <div id="email">
                <div class="info">
                    <label for="email"> Email*<br><br></label>
                    <input type="email" required>
                </div>
            </div>
            <div id="msg">
                <div class="info">
                    <label for="message"> Your Message*<br><br></label>
                    <textarea required id="textarea" cols="25" rows="5"></textarea> 
                </div>  
            </div>
            <div id="sub">
                    <input id ="defaultsub" type="submit">
            </div>
        </fieldset>
    </form> 
</div>

This is what I have so far and it prevents the default submission except it gets rid of the html5 validation.这是我到目前为止所拥有的,它阻止了默认提交,除非它摆脱了 html5 验证。 I was wondering how to have both in my code?我想知道如何在我的代码中同时拥有两者?

document.getElementById("defaultsub").addEventListener("click", function(event){
    event.preventDefault();
    console.log("Prevent default submission");
    alert("ALERT!!!!!");
})

Well, I don't know why you would want it to only prevent default submission when ALL required fields are filled IN.好吧,我不知道为什么您希望它仅在填写所有必填字段时阻止默认提交。

But, since you asked, you can use the document.querySelector('form') .checkValidity() method which will check if the form is valid.但是,既然你问了,你可以使用document.querySelector('form') .checkValidity()方法来检查表单是否有效。 In your case, you can use it like this在您的情况下,您可以像这样使用它

document.getElementById("defaultsub").addEventListener("click", function(event){
    if(document.querySelector('form').checkValidity()){
     event.preventDefault();
     console.log("Prevent default submission");
     alert("ALERT!!!!!");
}
})

This will prevent default form submission ONLY if the form is valid (AKA if all required fields are filled in)仅当表单有效时,这将阻止默认表单提交(如果所有必填字段都已填写)

EDIT:编辑:

You might have to run the checkValidity() on the required INPUT elements instead of the whole FORM element for this tto work, which will take a little more time to write.您可能必须在所需的 INPUT 元素而不是整个 FORM 元素上运行checkValidity()才能使此checkValidity()工作,这将需要更多的时间来编写。

Try the below, you need to check if any field is missing:尝试以下操作,您需要检查是否缺少任何字段:

document.getElementById("defaultsub").addEventListener("click", function(event){
      var els = document.querySelectorAll('input[type=text], input[type=email], textarea');
      let allFields=true;
      for(let i = 0; i < els.length; i++){
        if(!els[i].value.trim()){
          allFields=false;
          break;
        }
      }
      if (allFields) {
          event.preventDefault();
          console.clear();
          console.log("Prevent default submission");
      }
    })

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

相关问题 未填写必填字段时停止提交表单 - Stop form submission when the required fields have not been filled out 如果未填写必填字段,如何防止提交表单上的函数调用? - How to prevent function call on submit form if required fields are not filled out? 填写所有必填字段后启用保存按钮 - Enable save button when all the required fields are filled 如何检查表单中所有必填字段的填写时间? - How can I check when all of the required fields of a form has been filled out? 防止双重提交并允许“必填”字段 - Prevent double submission AND allow “required” fields 直到所有必填字段都填满后,如何使JavaScript警报不运行 - how to make javascript alert not run until all required fields are filled 如何禁用按钮直到所有必填字段都已填写 - how to disable a button unti all the required fields are filled 如何检查是否填写了所有必填字段以启用提交按钮? - How to check if all required fields are filled to enable submit button? 当所有输入文本字段为空时阻止表单提交,但当其中任何一个都不为空时允许使用jquery提交 - prevent form submission when all input text fields are empty but allow submission when any of them in not empty with jquery 检查是否在特定的div中填写了所有必填字段 - Check if all required fields are filled in a specific div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM