我试图在新选项卡中将表单数据提交到https://wallpapersforandroid.com/wallpaper-4/”,并在提交后将父页面重定向到“ https://www.youtube.com”。我成功在新标签页中打开操作页面,但父页面未重定向我使用的代码:
<form action="https://wallpapersforandroid.com/wallpaper-4/" method="post" target="_blank">
Type Password From Below Image: <input name="pass" type="text" />
<input id="myForm" type="submit" /><br />
<script>
document.getElementById("myForm").onsubmit = function() {
window.location.href = "https://www.youtube.com";
};
</script>
如果提交表单,则当前页面将被删除,并替换为提交表单的结果。如果设置了新位置,则当前页面将被拆除,并替换为该位置的页面。您不能一次做到这两者;其中一个赢了,另一个赢了。
您可以通过ajax提交表单,然后执行重定向:
document.getElemntById("myForm").addEventListener("submit", function(e) {
// Prevent the default form submission
e.preventDefault();
// Get the form data
const data = new FormData(this);
// Do the ajax
fetch(this.action, {
method: this.method,
body: data
})
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
window.location.href = "https://www.youtube.com";
})
.catch(error => {
// ...handle/report error...
});
});