我正在为我的注册表单开发Javascript页面。我只是不知道如果所有内容都填好了,如何将页面重定向到另一个页面。
document.getElementById("check").onclick = function() {
let allAreFilled = true;
document.getElementById("myForm").querySelectorAll("[required]").forEach(function(i) {
if (!allAreFilled) return;
if (!i.value) allAreFilled = false;
if (i.type === "radio") {
let radioValueCheck = false;
document.getElementById("myForm").querySelectorAll(`[name=${i.name}]`).forEach(function(r) {
if (r.checked) radioValueCheck = true;
})
allAreFilled = radioValueCheck;
}
})
if (!allAreFilled) {
alert('Fill all the fields');
}
else {
window.location.replace("http://www.w3schools.com");
}
};
我正在为同样可能的问题提供我的示例脚本,我希望这能帮助
HTML
<pre>
<!DOCTYPE HTML>
<html>
<body>
<p id=P >Fill in all checkboxes</p>
<form>
<input type=radio />
<label>Check 1</label>
<input type=radio />
<label>Check 2</label>
<input type=radio />
<label>Check 3</label>
</form>
</body>
</html>
</pre>
JavaScript
window.onload = () =>
{
var radio = document.querySelectorAll('[type=radio]'),
p = document.getElementById("P");
radio.forEach(
function(i)
{
i.onchange = () =>
{
var checked = document.querySelectorAll("[type=radio]:checked"),
leftover = radio.length - checked.length;
p.innerHTML = "leftover checkbox " + leftover;
if(radio.length == checked.length)
{
alert("All checked");
window.location.href = "https://ilhamb.com/";
}
}
}
)
}
此代码为
window.location = "w3schools.com"
或
window.location.href = "w3schools.com"
应该这样做
使用window.location.htm在同一选项卡/窗口内重定向,或使用window.open((在新选项卡/窗口中重定向。
正如Quentin所指出的,你已经在使用一种适当的重定向方式。我可以建议打开控制台并检查是否有控制台错误吗?还要确保您的程序通过使用浏览器断点或控制台日志真正到达重定向。