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

时间:2019-11-10 06:16:57

标签: javascript wordpress redirect elementor

目标: 在用户在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>

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

1 个答案:

答案 0 :(得分: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()

相关问题