JavaScript:如何在验证后重定向页面
我想在验证后重定向页面。我有最新消息。例如: form.html:JavaScript:如何在验证后重定向页面,javascript,html,validation,Javascript,Html,Validation,我想在验证后重定向页面。我有最新消息。例如: form.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'>&
<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 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>
函数检查表(){
if(document.getElementById(“un”).value='jayem30'和&document.getElementById(“pw”).value='jayem'){
警报(“登录成功”);
window.location=”http://www.google.com/"
}否则{
警报(“访问被拒绝。需要有效的用户名和密码。”);
}
}
我试过了,但它没有重定向到google.com。非常感谢您的帮助!:) 尝试document.location.href而不是window.location尝试将重定向设置为超时。很有魅力
setTimeout(function() {window.location = "http://www.google.com/" });
顺便说一下,不是
onsubmit="return checkform(this);"
使用
因为IE不喜欢在验证后使用ommit(and)。尝试此方法重定向页面 语法:
window.location.assign("url")
例如:
<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>
函数检查表(){
if(document.getElementById(“un”).value='jayem30'和&document.getElementById(“pw”).value='jayem'){
警报(“登录成功”);
window.location.assign(“http://www.google.com/")
}否则{
警报(“访问被拒绝。需要有效的用户名和密码。”);
}
}
这是一种有效的方法。应该是window.location.hrefdocument.location在所有浏览器中都不起作用。是的,我同意。跨浏览器功能使用window.location而不是document.location。
<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>