Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:如何在验证后重定向页面_Javascript_Html_Validation - Fatal编程技术网

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

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.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 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>