请告诉我,当用户名和密码与我在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.assign
null
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>
您可以使用window.location.replace(“student.html”)
替换当前文档。 也可以使用window.location.href=“student.html”;
。
null
<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>