目标:
在用户在Elementor中提交表单后将用户重定向到其他页面。重定向将基于存储的javascript变量。我给了提交按钮一个名为submitbutt
的ID。
实施:
对于Elementor中的Form,我选择了Actions after submit
至Redirect
。同样在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>
问题:用户点击“提交”按钮后,没有重定向发生。
答案 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()