如何防止在提交联系表 7 后没有输入必填字段后重定向到某个 URL

How to prevent redirect to a certain URL after submission of Contact Form 7 without input required fields

我的目标是在提交联系表 7 后重定向到外部 URL,但前提是所有必填字段都已填写。相反,我每次都会收到重定向,即使所有字段都已填写空白的。 没有输入的字段被标记为无效,但不会阻止重定向。

<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit" onclick="window.location.href='https://example.com'" />

有一些事件可以帮助您实现场景。在您的情况下,一旦发送了电子邮件,就应该调用事件,因此您可以为此使用 wpcf7mailsent

在页脚添加以下代码。

  <script type="text/javascript">

    document.addEventListener( 'wpcf7mailsent', function( event ) {
     window.location.href='https://example.com' 
    }, false );
  </script>

以上代码正确。如果仍然发生重定向,试试这个:

<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
 window.location.href='#' 
}, true );
</script>