javascript登录系统功能

时间:2019-03-19 17:33:16

标签: javascript jquery

我正在尝试构建功能性的登录/注册系统。 但是当我对其进行测试时,它总是显示我放置的错误警报,即使用户名正确和来自数组,它也会显示 我添加了。

var objPeople = [{ username: 'St226' }, { username: 'Mm151115' }, { username: 'Mobi8685' }, { username: 'Rmj203' }, { username: 'Afs8685' }, { username: 'Abo sma5' }]
var oldPeople = [{ username: 'St226' }, { username: 'Mm151115' }, { username: 'Mobi8685' }, { username: 'Rmj203' }, { username: 'Afs8685' }, { username: 'Abo sma5' }]
// login functionality
function login() {
    // retreive data from username and store in username variable
    var username = document.getElementById('username').value
    // loop through all the user pbjects and confrim if the username is correct
    for (var i = 0; i < objPeople.length; i++) {
        // check to 
        if (username == objPeople[i].username) {
            if (username == oldPeople[i].username) {
                window.location.href = "./" + username + ".html"

            } else {
                window.location.href = "./newuser.html"
            }
        } else {
            ErrorAlert();
            break
        }
    }
}

1 个答案:

答案 0 :(得分:0)

假设objPeople和oldPeople在两个数组的确切索引处具有相同的用户。

var objPeople = [{ username: 'St226' }, { username: 'Mm151115' }, { username: 'Mobi8685' }, { username: 'Rmj203' }, { username: 'Afs8685' }, { username: 'Abo sma5' }];

var oldPeople = [{ username: 'St226' }, { username: 'Mm151115' }, { username: 'Mobi8685' }, { username: 'Rmj203' }, { username: 'Afs8685' }, { username: 'Abo sma5' }];

 // login functionality
function login() {
  // retreive data from username and store in username variable
  var username = document.getElementById('username').value;

  // loop through all the user pbjects and confrim if the username is correct
  for (var i = 0; i < objPeople.length; i++) {
      // check to 
      if (username == objPeople[i].username) {
          if (username == oldPeople[i].username) {
              window.location.href = "./" + username + ".html"
          } 
      } 
  }
  window.location.href = "./newuser.html";
}