admin管理员组文章数量:1429688
I'm using a contact form for my WP website. I want to call some function before I submit the form.
I thought I could disable the submit form by using the following code:
let form = document.querySelector('form');
form.addEventListener('submit', function(ev){
ev.preventDefault();
//some more functions
});
For some reason this isn't working and CF7 is still submiting my form. Does someone know how to prevent the submit function.
I don't want to use the disabled function on the button cause this way the form loses it's orignal function.
I'm using a contact form for my WP website. I want to call some function before I submit the form.
I thought I could disable the submit form by using the following code:
let form = document.querySelector('form');
form.addEventListener('submit', function(ev){
ev.preventDefault();
//some more functions
});
For some reason this isn't working and CF7 is still submiting my form. Does someone know how to prevent the submit function.
I don't want to use the disabled function on the button cause this way the form loses it's orignal function.
Share Improve this question asked Apr 21, 2018 at 10:09 Dennis SpierenburgDennis Spierenburg 6532 gold badges6 silver badges16 bronze badges3 Answers
Reset to default 1I tried it on my page and had the same problem.
Try it with input[type=submit]
instead of form
.
let form = document.querySelector('input[type=submit]');
form.addEventListener('click', function(e) {
e.preventDefault();
})
Edit:
This would work if you only use 1 form in your HTML:
window.addEventListener('submit', function(e) {
e.preventDefault();
}, true)
Option is to block 'clicking' on the submit input. So not 100% what you needed, but for me it does the job.
Not sure if that is not against your condition to avoid 'disabled function on the button' but for me the form keeps the original function, just depends on user decision.
var submit = document.getElementsByClassName('wpcf7-submit')[0];
submit.addEventListener('click', function(e) {
var confirmMessage = 'If you require a response, please make sure you have pleted the ‘Name’ and ‘Email’ fields.';
if (!confirm(confirmMessage)) {
e.preventDefault();
}
});
<form method="post" class="wpcf7-form" enctype="multipart/form-data" novalidate="novalidate">
<p><label> Name<br>
<span class="wpcf7-form-control-wrap your-name"><input type="text" name="your-name" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false"></span> </label></p>
<p><label> Email address<br>
<span class="wpcf7-form-control-wrap your-email"><input type="email" name="your-email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-email" aria-invalid="false"></span> </label></p>
<p><input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit"><span class="ajax-loader"></span></p>
<div class="wpcf7-response-output wpcf7-display-none"></div>
</form>
Quickfix, should be refactored by extending this js. As UX i've added a GIF image instead of the button after first submit click.
For anyone who haven't found a solution in contact-form-7/includes/js/scripts.js search for line 499 now, probably changing meantime, you have wpcf7.clearResponse, inside in this function add :
var str = '<img src="path_of_file"/>';
$('.wpcf7-submit').replaceWith(str);
This will replace your button after click with the image with path path_of_file.
本文标签: javascriptDisable Contact Form 7 form submitStack Overflow
版权声明:本文标题:javascript - Disable Contact Form 7 form submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745506944a2661287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论