我想借助JavaScript创建一个简单的登录系统。按下Enter键后,我想在将John或Neil输入到文本框中后将页面重定向到google。帮助吗?
var objPeople = [
{studentid: "John"},
{studentid: "Neil"}
]
var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event){
for(i = 0; i < objPeople.length; i++){
if (myInput == objPeople[i].myInput && event.keyCode === 13){
window.location.href = "www.google.com";
}
}
});
<html>
<head>
<script LANGUAGE="JavaScript" src="JavaScript.js"></script>
<title>aaa</title>
</head>
<body>
<input type="text" id="myInput">
</body>
</html>
答案 0 :(得分:0)
您必须采用input的值,并使用存储元素的变量的名称和id名称。要访问对象属性,您必须使用属性名称而不是输入的ID。
input.value == objPeople[i].studentid
var objPeople = [
{studentid: "John"},
{studentid: "Neil"}
]
var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event){
for(i = 0; i < objPeople.length; i++){
if (input.value == objPeople[i].studentid && event.keyCode === 13){
window.location.href = "http:\\www.google.com";
}
}
});
<html>
<head>
<script LANGUAGE="JavaScript" src="JavaScript.js"></script>
<title>aaa</title>
</head>
<body>
<input type="text" id="myInput">
</body>
</html>