JavaScript:如何在验证后重定向页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5958936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: How to redirect a page after validation
提问by Kris
I want to redirect a page after validation. I have the ff. EXAMPLE:
我想在验证后重定向页面。我有 ff。例子:
form.html:
表单.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>
script:
脚本:
<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>
I tried this but it does not redirect to google.com. Many thanks for any help! :)
我试过这个,但它没有重定向到 google.com。非常感谢您的帮助!:)
采纳答案by Vilius 1989
Try putting redirect in a timeout. Works like a charm
尝试将重定向置于超时状态。奇迹般有效
setTimeout(function() {window.location = "http://www.google.com/" });
By the way, instead of
顺便说一句,而不是
onsubmit="return checkform(this);"
use
利用
onsubmit="return(checkform())"
because IE doesn't like when you ommit ( and ).
因为 IE 不喜欢您省略 ( 和 )。
回答by nirav
Try out this method to redirect page after validation.
尝试使用此方法在验证后重定向页面。
syntax:
句法:
window.location.assign("url")
example:
例子:
<script type='text/javascript'>
function checkform(){
if(document.getElementById("un").value == 'jayem30' && document.getElementById("pw").value == 'jayem' ){
alert("Login Successful");
window.location.assign("http://www.google.com/")
}else{
alert("Access denied. Valid username and password is required.");
}
}
</script>
This is a working method.
这是一种工作方法。
回答by Amareswar
try document.location.href instead of window.location
尝试 document.location.href 而不是 window.location