Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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 HTML登录不工作_Javascript_Html_Login - Fatal编程技术网

Javascript HTML登录不工作

Javascript HTML登录不工作,javascript,html,login,Javascript,Html,Login,我正试图让自己登录我的网站,我已经测试了提供的链接存在,重定向工作。我不知道为什么它不起作用! 代码如下: <!DOCTYPE html> <html lang="en"> <head> <title>login2</title> <meta charset="utf-8" /> </head> <body> <input class="textBox" id="pas

我正试图让自己登录我的网站,我已经测试了提供的链接存在,重定向工作。我不知道为什么它不起作用! 代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>login2</title>
    <meta charset="utf-8" />
</head>

<body>
    <input class="textBox" id="pass" type="password" maxlength="30" required/>
    <button type="button" onclick="a()">Login</button>
    <script>
        function a() {
            var i = document.getElementById('pass') if (i == "1234") {
                window.location = "in.html"
            } else {
                window.location =
                    "index.html"
            }

        }
        }
    </script>
</body>
</html>

我到处找,想弄明白为什么它不起作用。请帮帮我

尝试改用此脚本;替换整个脚本块;缺少了一些分号。正如@dfsq提到的,还有额外的}。正如@spencer所说,您需要.value来获取密码文本

<script>
    function a() {
        var i = document.getElementById('pass').value;
        if (i == "1234") {
            window.location = "in.html";
        } 
        else {
            window.location = "index.html";
        }
    }
</script>

请注意,我建议不要检查客户端的密码,因为客户端工具可以对其进行操作。

对不起!每行前面的“>”都是因为这个网站上的文本编辑器!格式化代码后,很明显您有额外的}。我要求您首先学习php登录系统您可能想做的事情:i=document.getElementById'pass'.value,因为在这种情况下,我永远不会是1234,因为您得到的是DOM对象。另外,请描述确切的问题。更新了更多信息。太好了!在JavaScript中,分号在很大程度上是可选的。没有它们不会损害代码。是的。我同意。它提高了可读性,特别是当你像最初一样在一行中有多个语句时。谢谢!!!!!我知道它不太安全,但它是我正在制作的一个非常基本的小网站。再次感谢,这真的很有帮助OP没有使用jQuery。对于这种解决方案,jQuery是一种滥杀滥伤,在这种情况下,它会降低性能,因为它必须加载整个jQuery库。如果这个表单是他唯一使用过的东西-也许,但我有点怀疑。此外,这是一种比上述方法更好、更专业的方法。
<!DOCTYPE html>
<html lang="en">
<head>
    <title>login2</title>
    <meta charset="utf-8" />
    <script src="//code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="loginForm" method="post">
        <input class="textBox" id="pass" type="password" maxlength="30" required/>
        <input type="submit" name="submit" value="login">
    </form>
    <script>
        $('#loginForm').on('submit', function(e) {
            e.preventDefault();
            var i = $('#pass').val();
            if (i == "1234") {
                window.location = "in.html";
            } else {
                window.location = "index.html";
            }
        });
    </script>
</body>
</html>