-1

今天我开始学习 javscript,但是当我尝试创建一个非常基本的登录功能时,它不起作用。所以我试图使用一个数组来存储用户名和另一个用于密码,但是当我输入它们时,它会出现错误的用户名或传递帮助。

这是代码

var usernames=new Array(); 
 usernames[0]="user";       
 usernames[1]="sam";
 usernames[2]="bob";
 var passwords=new Array(); 
 passwords[0]="pass";       
 passwords[1]="pass1";
 passwords[2]="pass19";

var neworold=prompt("sign in or create account"); 


if (neworold > 8 ){
var createu=prompt("Please enter your desired username");
  var createp=prompt("Please enter your desired password");

 } else {
 var username=prompt("Please enter your username");
 var password=prompt("Please enter your password");}

if (username == usernames) {
 if (password == passwords) {
 alert("");
}
} else {
alert("wrong username or pass");
}
4

1 回答 1

1

试试这个:- http://jsfiddle.net/aidioo7/RE3Db/1/

JS:-

var usernames = new Array();
usernames[0] = "user";
usernames[1] = "sam";
usernames[2] = "bob";
var passwords = new Array();
passwords[0] = "pass";
passwords[1] = "pass1";
passwords[2] = "pass19";

var neworold = prompt("sign in or create account");


if (neworold.length > 8) {
    var createu = prompt("Please enter your desired username");
    var createp = prompt("Please enter your desired password");

} else {
    var username = prompt("Please enter your username");
    var password = prompt("Please enter your password");
}

if (usernames.indexOf(username) != -1) {
    if (passwords[usernames.indexOf(username)] == password) {
        alert("");
    }
} else {
    alert("wrong username or pass");
}
于 2013-09-20T05:30:47.030 回答