[英]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.