【问题标题】:Javascript won't redirect pageJavascript不会重定向页面
【发布时间】:2021-03-13 09:23:11
【问题描述】:

仅供参考:我真的不知道如何使用 StackOverflow,因此某些命令(例如 on click 或其他命令)我将其更改为大写,这样 Stack 不会在尝试发布此内容时向我抛出错误。

这是我在提交表单后尝试将页面重定向到另一个页面的代码。但这对我不起作用。似乎所有代码都完成了所有功能,只是在正确输入 PIN 后它不会重定向到另一个页面。

HTML 代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link ="stylesheet" href="/static/stylesheets/index.css?v=1.0.0">
    <title>MAID - Homepage</title>
</head>

<body>
    <div id="message">
        <h1>Welcome to MAID</h1>
        <h3>Please enter your PIN</h3>

        <form name="pinForm" class="pinSubmit" onSubmit="return toLobby()" method="POST">
            <label for="pin">PIN</label><br>
            <input type="password" id="pinText" name="pinInput" onKeypress="return inputChange()">
            <input id="enter" type="submit" value="Enter!">
        </form> <br>
        <div class="createRoomClass">
            <button id="createRoom" onClick="randomPin()">Create Room</button>
        </div>
    </div>
</body>
</html>

JS 代码

var pinHolder = [];

function randomPin() {
    var random = Math.floor(100000 + Math.random() * 900000);
    pinHolder.push(parseInt(random));

    window.alert(random);

    console.log(pinHolder);
    console.log(typeof(random));
    console.log(pinHolder[pinHolder.length - 1]);
}

function toLobby() {
    // var formPIN = document.forms["pinForm"]["pinInput"].value;

    // console.log(formPIN);

    // if (formPIN == pinHolder[-1]) {
    //     window.location.replace("http://127.0.0.1:5000/lobby");
    // } else if (formPIN == "") {
    //     alert("No PIN has enter");
    // } else if (formPIN != pinHolder[-1]) {
    //     alert("Wrong PIN");
    // }

    var enterPIN = parseInt(document.getElementById("pinText").value);

    if (enterPIN == pinHolder[pinHolder.length - 1]) {
        alert("Successfully enter");
        window.location.href = "lobby.html";
        return true;
    }
    else {
        alert("Wrong PIN or no PIN has enter")
        return false;
    }
    console.log(enterPIN);
}

function inputChange() {
    var content = document.getElementById("pinText").value;
    return content.length < 6;
}

我也试试这个

window.location.href = "";
window.location.replace();

【问题讨论】:

    标签: javascript html forms web-applications


    【解决方案1】:

    这对我有用:

    self.location = "http://..."
    

    http://net-informations.com/js/iq/load.htm

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2013-07-07
      • 2013-11-03
      相关资源
      最近更新 更多