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

标签 javascript html validation

我想在验证后重定向一个页面。我有 ff。示例:

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

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

最佳答案

尝试将重定向置于超时状态。像魅力一样工作

setTimeout(function() {window.location = "http://www.google.com/" });

顺便说一下,而不是

onsubmit="return checkform(this);"

使用

onsubmit="return(checkform())"

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

关于JavaScript:如何在验证后重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5958936/

相关文章:

javascript - 有没有办法改进算法?

javascript - React - 文档标题添加图标

jquery - 不使用模型的 ASP.NET MVC ajax 服务器端验证

支持多个项目拖放的 Javascript TreeView

javascript - Infragistics - 在代码中显示/隐藏列(不使用默认列选择器对话框)

javascript - 响应式粘性汉堡导航栏

jquery - 如何使用 img 标签在不拉伸(stretch)移动设备的情况下全屏显示 Bootstrap slider 图像?

javascript - 我正在尝试让表单验证与 Angular 向导一起使用

c# - FluentValidation 和嵌套验证器

java - 如何将数组复制到phonegap中的文件中?