javascript - 联系表格 7 - 重定向到另一个页面

标签 javascript jquery contact-form-7

我的 WordPress 网站中有两种表单。我需要在仅提交一个表单后将用户重定向到另一个页面。因此尝试使用此 javascript if 条件与联系表单 7 ID。

 document.addEventListener( 'wpcf7mailsent', function( event ) { 
    setTimeout(function(){
        location = 'https://example.com/';
    }, 2500);
}, false );

以上代码适用于所有页面,因此我用 if 条件这样修改了它。

document.addEventListener( 'wpcf7mailsent', function( event ) { 
  if('2168' == event.detail.contactFormId){
    setTimeout(function(){
        location = 'https://example.com/';
    }, 2500)  } ;
}, false );

但令人惊讶的是它不起作用。有什么想法吗?

最佳答案

Above code applied for all pages so I modified it with if condition this way.

这效率很低,您应该使用以下方式定位显示表单的页面:

add_filter('do_shortcode_tag',  'redirect_form_script', 10,3);
function redirect_form_script($output, $tag, $attrs){
    //check this is your form, assuming form id = 1, replace it with your id.
    if($tag != "contact-form-7" || $attrs['id']!= 2168) return $output;
    $script = '<script>'.PHP_EOL;
    $script .= 'document.addEventListener( "wpcf7mailsent", function( event ){'.PHP_EOL;
    //add your redirect page url.
    $script .= '    location = "http://example.com/submitted/?cf7="'.PHP_EOL;
    $script .= '  }'.PHP_EOL;
    $script .= '</script>'.PHP_EOL;
    return $output.PHP_EOL.$script;
  }

关于javascript - 联系表格 7 - 重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60885762/

相关文章:

javascript - 如何根据单击的不同按钮获得不同的 $_POST 值

javascript - HTML 半表是可滚动的,而其他是固定的

wordpress - 我正在尝试将 PayPal Payment 与 WordPress 中的 Contact Form 7 集成,但我的重定向无法正常工作

javascript - Bootstrap DateTimePicker 和 jQuery 问题

javascript - 如何测试 "prevObject"?

wordpress - 在 Contact Form 7 插件中选择国家和自动生成电话代码 - wordpress

html - 联系表 7 - 选择选项透明

javascript - `bindAll` 过时了吗?

javascript - 如何格式化 bootstrap 4 按钮以在按钮的不同位置显示部分文本

javascript - 单击外部关闭 div(父级)