[英]I am not able to load another html page which I have created as student.html after javascript validation
Please tell me which function I can use to open student.html when the username and password is matching with the value I described in scrip tag.请告诉我当用户名和密码与我在脚本标签中描述的值匹配时,我可以使用哪个 function 打开 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>
You can use window.location.replace("student.html")
to replace the current document.您可以使用window.location.replace("student.html")
替换当前文档。 Or you can use window.location.href = "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>
Use:利用:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.