如何验证 Contact Form 7 提交

How to validate Contact Form 7 Submit

我尝试在提交表单时向 GA 发送事​​件,但在测试时,我发现即使某些必填字段未填写,wpcf7submit 也会发送事件。

"wpcf7submit — Fires when an Ajax form submission has completed successfully, regardless of other incidents."

我已经用警报对其进行了测试,每当我没有填写所有必填字段时,我仍然会收到警报

var wpcf7Elm = document.querySelector( '.wpcf7' );

wpcf7Elm.addEventListener( 'wpcf7submit', function( event ) {
    alert( "Fire!" );
}, false );
var wpcf7Elm = document.querySelector('.wpcf7');

wpcf7Elm.addEventListener('wpcf7submit', function (event) {
    ga('send', 'event', 'Lead Form', 'submit');
}, false);

如果您只对成功感兴趣,您可能想尝试 wpcf7mailsent 活动。

var wpcf7Elm = document.querySelector('.wpcf7');

wpcf7Elm.addEventListener('wpcf7mailsent', function (event) {
    ga('send', 'event', 'Lead Form', 'submit');
}, false);

wpcf7mailsent — Fires when an Ajax form submission has completed successfully, and mail has been sent.