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

How to redirect users after form submission?

Objective: 在 Elementor 中提交表单后将用户重定向到不同的页面。重定向将基于存储的 javascript 变量。我已经为提交按钮提供了一个名为 submitbutt.

的 ID

实施: 对于 Elementor 中的表单,我选择了 Actions after submitRedirect。同样在 Redirect 部分,我选择了 to Shortcode 选项并参考了我的简码 [elementor-template id="1343"].

简码

它是一个 HTML,其中包含一个 javascript,看起来像这样。

<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>

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

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()