【问题标题】:Javascript redirection to other page not workingJavascript重定向到另一个页面不起作用
【发布时间】:2019-05-28 13:58:51
【问题描述】:

我正在使用联系表格 7 插件的 WordPress 网站工作。现在我想将页面重定向到感谢页面并在表单 ID 后面附加 URL。但每次它重定向到主页。我正在使用 wpcf7mailsent DOM 事件。

我已经搜索了几个小时。我使用了以下重定向方法

window.location("/thankyou?submission="+formID);
document.location = 'https://example.com/thankyou'+formID;
document.location.href = '/thankyou?submission='+formID;
window.location.assign('https://example.com/thankyou'+formID);
window.location.replace('https://example.com/thankyou'+formID);
window.location.replace('https://example.com/thankyou'+formID);
jQuery(location).attr('href','example.com/thankyou'+formID);

这是我的代码

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

nr_wpcf7Elm.addEventListener( 'wpcf7mailsent', function( event ) {
    var inputs = event.detail.inputs;
    var formID = event.detail.contactFormId;
    window.location.href = '/thankyou?submission='+formID;
}, false );

我需要将用户重定向到感谢页面,并且我想要带有表单 ID 的 URL。

【问题讨论】:

  • 这个问题很可能是因为formID未定义。
  • formID 在这里定义 var formID = event.detail.contactFormId;它具有价值
  • 已声明并设置,但很可能未定义。请 console.log() 它并告诉我们结果。
  • 控制台显示值
  • 先把它全部弄出来:var linkText = '/thankyou?submission='+formID; console.log(linkText);

标签: javascript wordpress


【解决方案1】:

尝试使用wpcf7submit 事件。

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

nr_wpcf7Elm.addEventListener( 'wpcf7mailsubmit', function( event ) {
    var inputs = event.detail.inputs;
    var formID = event.detail.contactFormId;
    window.location.href = '/thankyou?submission='+formID;
}, false );

【讨论】:

  • 我需要它在 wpcf7mailsent 上运行。不在 wpcf7mailsubmit 上
猜你喜欢
  • 1970-01-01
  • 2020-06-12
  • 2018-04-29
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 2013-08-16
  • 1970-01-01
相关资源
最近更新 更多