Javascript 提交表单后如何重定向用户?
目标: 在Elementor中提交表单后将用户重定向到不同页面。重定向将基于存储的javascript变量。我给了提交按钮一个名为Javascript 提交表单后如何重定向用户?,javascript,wordpress,redirect,elementor,Javascript,Wordpress,Redirect,Elementor,目标: 在Elementor中提交表单后将用户重定向到不同页面。重定向将基于存储的javascript变量。我给了提交按钮一个名为submitbutt的ID 实施: 对于Elementor中的表单,我选择了提交后的操作到重定向。同样在重定向部分,我选择了到短代码选项,并参考了我的短代码[elementor template id=“1343”] 短代码 它是一个包含javascript的HTML,看起来像这样 <script type="text/javascript"> doc
submitbutt
的ID
实施:
对于Elementor中的表单,我选择了提交后的操作
到重定向
。同样在重定向
部分,我选择了到短代码
选项,并参考了我的短代码[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>
document.getElementById('submitbutt')。onclick=function(){
if(localStorage.getItem('pagex')==“第1页”){
window.location=https://www.website.com/error/?1';
}
else if(localStorage.getItem('pagex')=='page_2'){
window.location=https://www.website.com/error/?2';
}
否则{
window.location=https://www.website.com/error/';
}
};
问题:用户点击提交按钮后,不会发生重定向
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/';
}
});
使用带有按钮的事件处理程序
对于表单,要自定义按钮的行为,请使用