在 javascript 验证后,我无法加载我创建为 student.html 的另一个 html 页面

I am not able to load another html page which I have created as student.html after javascript validation

当用户名和密码与我在 scrip 标签中描述的值匹配时,请告诉我可以使用哪个函数打开 student.html。

<form>
    <input
        name="username"
        type="text"
        id="txt_username"
        class="text1"
        ondrop="return false;" 
        onpaste="return false;"
        style="color:#0000C0;border-style:Solid;font-family:verdana;font-size:Small;"
    />
    <input
        name="password"
        type="password"
        id="txt_password"
        class="text1"
        style="color:#0000C0;background-color:White;;border-style:Solid;font-family:verdana;font-size:Small;"
    />  
    <input
        type="submit"
        value="Login"
        onclick="validate()"
    />
</form>

<script type="text/javascript">
function validate() {
    var text1 = document.getElementById("txt_username");
    var text2 = document.getElementById("txt_password");
    if (text1.value == "root" && text2.value == "root") {
        alert("ok")
        load("student.html");
    } else {
        alert("fail");
        load("error.html");
    }
}
</script>

您可以使用window.location.replace("student.html")替换当前文档。或者你可以使用 window.location.href = "student.html";.

<form onsubmit="return validate(event)">
    <input
        name="username"
        type="text"
        id="txt_username"
        class="text1"
        ondrop="return false;" 
        onpaste="return false;"
        style="color:#0000C0;border-style:Solid;font-family:verdana;font-size:Small;"
    />
    <input
        name="password"
        type="password"
        id="txt_password"
        class="text1"
        style="color:#0000C0;background-color:White;;border-style:Solid;font-family:verdana;font-size:Small;"
    />  
    <input
        type="submit"
        value="Login"
    />
</form>
<script>
function validate(event) {
    event.preventDefault();
    var text1 = document.getElementById("txt_username");
    var text2 = document.getElementById("txt_password");
    if (text1.value == "root" && text2.value == "root") {
        window.location.replace("student.html");        
    } else {
        window.location.replace("error.html");                
    }
}
</script>

使用:

 window.location.assign


function validate() {
    var text1 = document.getElementById("txt_username");
    var text2 = document.getElementById("txt_password");
    if (text1.value == "root" && text2.value == "root") {
        window.location.assign("student.html");        
    } else {
        window.location.assign("error.html");                
    }
}
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<form>
    <input
        name="username"
        type="text"
        id="txt_username"
        class="text1"
        ondrop="return false;" 
        onpaste="return false;"
        style="color:#0000C0;border-style:Solid;font-family:verdana;font-size:Small;"
    />
    <input
        name="password"
        type="password"
        id="txt_password"
        class="text1"
        style="color:#0000C0;background-color:White;;border-style:Solid;font-family:verdana;font-size:Small;"
    />  
    <input
        type="submit"
        value="Login"
        onclick="validate()"
    />
</form>
</body>
</html>