我使用JavaScript DOM制作了一个注册/登录系统 每当有人注册要使用uName和uPassword键添加对象时,我都会告诉它 到一个数组。 它添加了它,并在console.log中添加了3个对象。 它们的键值不同。
但是当我“登录”时,它仅用于数组中的第一个对象。
代码:
var regFlag = true;
var userNameLog = document.getElementById('logUserName');
var userPassLog = document.getElementById('logUserPass');
function loggedIn() {
console.log('into log !');
console.log(userNameLog.value);
console.log(userPassLog.value);
console.log(localStorage.getItem('usersArray'));
var backToArr = JSON.parse(localStorage.getItem('usersArray'));
console.log(backToArr);
if (userNameLog.value != '' && userPassLog.value != '') {
for (var i = 0; i < backToArr.length; i++) {
console.log(backToArr[i]);
for (var key in backToArr[i]) {
console.log(key);
if (backToArr[i].uName == userNameLog.value && backToArr[i].uPassword == userPassLog.value) {
console.log('into successful login');
document.getElementById('inputAnswer').removeAttribute('disabled');
document.getElementById('subLogBtn').setAttribute('data-dismiss', 'modal');
} else {
document.getElementById('logMsgUser').innerHTML = '<p>השם משתמש או הסיסמא לא נכונים</p>';
userNameLog.value = '';
userPassLog.value = '';
}
}
}
} else {
document.getElementById('logMsgUser').innerHTML = '<p>השם משתמש או הסיסמא לא נכונים</p>';
}
}
/* end of login disaple or not */
function showRegister() {
document.getElementById('logInForm').style.display = 'none';
document.getElementById('regForm').style.display = 'block';
}
var userName = document.getElementById('regUserName');
var userPass = document.getElementById('regUserPass');
var userPassVali = document.getElementById('regUserPassVali');
function register() {
console.log(userName.value);
console.log(userPass.value);
console.log(userPassVali.value);
if (userPassVali.value != userPass.value) {
document.getElementById('signUpMsg').innerHTML = '<p>סיסמאות לא תואמות</p>';
} else {
var usersObj = {
uName: userName.value,
uPassword: userPass.value
}
usersArr.push(usersObj);
}
var strUserArr = JSON.stringify(usersArr);
console.log(strUserArr);
if (typeof(Storage) != undefined) {
localStorage.setItem('usersArray', strUserArr)
}
// console.log(localStorage);
console.log(usersArr);
console.log(usersArr.length)
console.log(usersObj);
// console.log(usersObj.uName)
}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter" id="modBtn">
התחברות/הרשמה
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
</div>
<div class="modal-body">
<form id="logInForm">
<div class="form-group">
<input type="text" id="logUserName" placeholder="Username">
<input type="text" id="logUserPass" placeholder="Password">
<div id="logMsgUser"></div>
<a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-content="Popover body content is set in this attribute." id="subLogBtn">שלח</a>
</div>
</form>
<form id="regForm">
<div class="form-group">
<input type="text" id="regUserName" placeholder="Username">
<input type="text" id="regUserPass" placeholder="Password">
<input type="text" id="regUserPassVali" placeholder="Validate Password">
<div id="signUpMsg"></div>
<a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-content="Popover body content is set in this attribute." id="subRegBtn">שלח</a>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" id="registerBtn">הירשם</button>
<button type="button" class="btn btn-primary" id="logBtn">התחבר</button>
</div>
</div>
</div>
</div>
</div>