提交表单后如何重定向用户?

问题描述 投票:0回答:1

目标:在用户在Elementor中提交表单后将用户重定向到其他页面。重定向将基于存储的javascript变量。我给了提交按钮一个名为submitbutt的ID。

实施:对于Elementor中的Form,我选择了Actions after submitRedirect。同样在Redirect部分中,我选择了to Shortcode选项并引用了我的短代码[elementor-template id="1343"]

简码

这是一个包含JavaScript的HTML,看起来像这样。

<script type="text/javascript">
  document.getElementById('submitbutt').onclick = function() {
if (localStorage.getItem('pagex') === 'page_1') {
 window.location = 'https://www.website.com/error/?1';
}
else if (localStorage.getItem('pagex') === 'page_2') {
  window.location = 'https://www.website.com/error/?2';
}
else {
 window.location = 'https://www.website.com/error/';
}
  };

</script>

问题:用户单击提交按钮后,没有重定向发生。

javascript wordpress redirect elementor
1个回答
0
投票
document.getElementById('submitbutt').addEventListener("click", function(event) {
event.preventDefault();
if (localStorage.getItem('pagex') === 'page_1') {
 window.location = 'https://www.website.com/error/?1';
}
else if (localStorage.getItem('pagex') === 'page_2') {
  window.location = 'https://www.website.com/error/?2';
}
else {
 window.location = 'https://www.website.com/error/';
}
});

使用带有按钮的事件处理程序

对于表单,要自定义按钮的行为,请使用event.preventDefault()

© www.soinside.com 2019 - 2024. All rights reserved.