[英]Contact form, disable submit button after clicked
I downloaded a working contact form and added a few things that I needed, but now I have another problem that I do not know how to fix it. 我下载了一个有效的联系表,并添加了一些我需要的东西,但是现在我遇到了另一个我不知道如何解决它的问题。
After form is submitted, the button can still be clicked a few times and that will send more than one email to me. 提交表单后,仍然可以单击该按钮几次,这将向我发送多封电子邮件。 After form is submitted, it should disable the button.
提交表单后,应禁用该按钮。
HTML: HTML:
<button type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
And here is the JavaScript code: 这是JavaScript代码:
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Did you fill in the form properly?");
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
Disable the button on form submit, like this: 禁用表单提交按钮,如下所示:
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Did you fill in the form properly?");
} else {
// everything looks good!
event.preventDefault();
$("#form-submit").prop("disabled",true);
submitForm();
}
});
Multiple things can be done here depending on your complete architecture. 根据您的完整体系结构,此处可以完成多项操作。 You can use jQuery's one function, although I don't recommend it, because this will mean that until you load the page the contact form can be submitted only once.
您可以使用jQuery的一个功能,尽管我不推荐这样做,因为这意味着在您加载页面之前,联系表格只能提交一次。
What I do is, that I would disable the button, $("#form-submit").attr("disabled","disabled");
我要做的是禁用按钮
$("#form-submit").attr("disabled","disabled");
OR $("#form-submit").addClass("inactive")
and in your CSS use 或
$("#form-submit").addClass("inactive")
并在CSS中使用
#form-submit{
pointer-events:none;
opacity:0.6;
/*etc etc*/
}
and when your ajax returns just do the opposite(remove the disabled attribute or class). 当您的ajax返回时,请执行相反的操作(删除禁用的属性或类)。 Also, set the input, textarea values to empty string.
另外,将输入的textarea值设置为空字符串。
const $contactForm=document.getElementById("contactForm");
const $submitButton=$contactForm.getElementById("form-submit");
$contactForm.addEventListener("submit",e => {
e.preventDefault();
$submitButton.setAttribute("disabled","disabled") }) //first is attribute, second is value, disabled
Javascript is simple, keep it simple. Javascript很简单,请保持简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.