简体   繁体   English

未填写必填字段时停止提交表单

[英]Stop form submission when the required fields have not been filled out

I have the following function with the problem that, when submitting the form, the redirection works even though the user has not filled in the required fields, do you know how to solve it?我有以下function的问题,提交表单时,即使用户没有填写必填字段,重定向仍然有效,您知道如何解决吗?

var submitButton = document.getElementById("form-submit");
var submitEvent = function (event) {

  var SiteTestRedirect = document.getElementById("SiteTestfs").value;
  var SiteTestRedirectPopup = document.getElementById("SiteTestfspop").value;

  if (SiteTestRedirect === "I Wanna Work" || SiteTestRedirectPopup === "I Wanna Work As --") {
    event.preventDefault();
    location = "https://SiteTest.es/thank-you-start-with-us/";
  } else if (SiteTestRedirect === "Work" || SiteTestRedirectPopup === "Work") {
    event.preventDefault();
    location = "http://SiteTest.es/thank-you-start-with-us/";
  } else {
    setTimeout(function(){
      location = "https://SiteTest.es/thank-you/";
    }, 3500);
  }


};

submitButton.addEventListener("click", submitEvent, false);

this is what i tried but still the form is sent with the required fields unfilled:这是我尝试过的,但仍然发送了未填写必填字段的表单:

function manualValidate(ev) {
  ev.target.checkValidity();
  return false;
}
$(".wpcf7-form").bind("submit", manualValidate);

It does not work because you change the location of the page on click.它不起作用,因为您在单击时更改了页面的位置。 It has no relationship to the form submission.它与表单提交无关。 The click action completes.点击动作完成。

So either call checkValidity inside your function that you call on click or move the code into the function you call on submit.因此,要么在单击时调用的 function 中调用 checkValidity,要么将代码移动到在提交时调用的 function 中。

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

相关问题 如何检查表单中所有必填字段的填写时间? - How can I check when all of the required fields of a form has been filled out? 填写所有必填字段时如何防止默认提交 - How to prevent default submission when ALL required fields are filled in 提交表单之前,请检查是否已填写某些字段 - Check to see if certain fields have been filled out before a form is submitted 使用Redux表单,如果未填写任何字段,如何禁用提交按钮? - Using Redux Form, how can I disable the submit button if no fields have been filled out? 填写表单字段后如何从中删除错误? - How to remove error from form fields after they have been filled out? 提交未填写必填字段的表单时无法停止函数执行 - Can't stop function execution when submitting a form with required fields not filled 检查所有输入字段是否已用jQuery填写 - Check all input fields have been filled out with jQuery 如何强制AngluarJS表单实现必需的字段已通过DOM操作填充(无用户输入)? - How can I force an AngluarJS form to realize required fields have been filled through DOM manipulation (no user input)? 如果未填写必填字段,如何防止提交表单上的函数调用? - How to prevent function call on submit form if required fields are not filled out? HTML 必填字段在未填写时不显示消息 - HTML required fields are not showing a message when it's not filled out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM