JavaScript:如何在验证后重定向页面

Kri*_*ris 5 html javascript validation

我想在验证后重定向页面。我有f. 例子:

表单.html:

<form method="post" action="" enctype="multipart/form-data" onsubmit="return checkform(this);">
<p><span style="width:180px">Username: </span><input type="text" name="username" id='un'></p>
<p><span style="width:180px">Password: </span><input type="password" name="password" id='pw'></p>
<input type="submit" value="SUBMIT" />
</form>
Run Code Online (Sandbox Code Playgroud)

脚本:

<script type='text/javascript'>
function checkform(){
    if(document.getElementById("un").value == 'jayem30' && document.getElementById("pw").value == 'jayem' ){
        alert("Login Successful");
        window.location = "http://www.google.com/"
    }else{
        alert("Access denied. Valid username and password is required.");
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

我尝试过,但它没有重定向到 google.com。非常感谢您的帮助!:)

Vil*_*989 4

尝试将重定向设置为超时。奇迹般有效

setTimeout(function() {window.location = "http://www.google.com/" });
Run Code Online (Sandbox Code Playgroud)

顺便说一句,而不是

onsubmit="return checkform(this);"
Run Code Online (Sandbox Code Playgroud)

使用

onsubmit="return(checkform())"
Run Code Online (Sandbox Code Playgroud)

因为 IE 不喜欢你省略 ( 和 )。