twitter-bootstrap 命令“window.location.href”不适用于 HTML(引导框架)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34011705/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:24:06  来源:igfitidea点击:

Command "window.location.href" not working on HTML (Bootstrap framework)

javascripthtmltwitter-bootstrap

提问by Rodolfo Rondon

I'm currently working on a university project in which I need to create a simple login screen which redirects the user to another landing page. As I'm new to web programming, I searched some functions and found that the most used command is the window.location.href, which simply doesn't work on my code. Here it is:

我目前正在从事一个大学项目,我需要创建一个简单的登录屏幕,将用户重定向到另一个登录页面。由于我是 Web 编程的新手,我搜索了一些函数,发现最常用的命令是window.location.href,它根本不适用于我的代码。这里是:

<button class="btn btn-lg btn-primary btn-block" type="submit" onclick="validation(this.form)">Sign in</button>
  </form>

<script language = "Javascript">
    function validation(form){
        if(form.inputEmail.value=="[email protected]" && form.inputPassword.value=="user123"){
                window.location.href('form.html');
        } 
        else {
            alert("Invalid e-mail or password.");
        }           
    }
</script>

I've already checked the paths, but the only method that works when you enter the valid username and password is the window.open()

我已经检查了路径,但是当您输入有效的用户名和密码时,唯一有效的方法是 window.open()

回答by Timeout

window.location.hrefis a property not a function. Assign the value as an absolute path instead.

window.location.href是一个属性而不是一个函数。将该值指定为绝对路径。

window.location.href = '/form.html';

There is also a function style way of doing this which you might be confusing it with:

还有一种函数风格的方法可以做到这一点,你可能会混淆它:

window.location.assign("form.html");

回答by Clay

It is not a function. Do this instead:

它不是一个函数。改为这样做:

window.location.href = 'form.html';

Additionally, turn on your browser's debug console to see any JavaScript errors. In Chrome it will say "Uncaught TypeError: window.location.href is not a function(…)"

此外,打开浏览器的调试控制台以查看任何 JavaScript 错误。在 Chrome 中它会说“Uncaught TypeError: window.location.href is not a function(...)”

回答by claudios

Try also this

也试试这个

window.location = "form.html";