我正在尝试构建登录页面,并在成功登录后在选择下拉列表中列出所有用户。我正在执行以下操作:
document.ready
上,我正在检查登录凭据,如下所示:$.ajax({
url: ‘ /api/login
',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(args),
success: function(data) {
if (!data.error) {
console.log("You are logged in!", data.result);
loadUsers();
} else {
console.log(data.error);
}
}
},
});
loadUsers()
如下:function loadUsers() {
$.ajax({
url: '/adminapi/getlogins',
dataType: 'json',
type: 'post',
contentType: 'application/json',
async: true,
success: function(data) {
if (!data.error) {
console.log("loaded select", data.result)
$.each(data.result, function(k, v) {
$('<option>').val(v.loginid).text(v.firstname + "" + v.familyname)
.appendTo('#selectLogin');
loginUserList.push(data.result);
});
} else {
console.log(data.error);
}
},
});
}
loginUserList
全局定义为空数组。
登录时,我看到控制台中loginUserList
users: (2) [{…}, {…}]
0: {loginid: 1, logintype: 2, firstname: "abc", familyname: "aaa", email: "aa@in.com"}
1: {loginid: 2, logintype: 1, firstname: "king", familyname: "abc", email: "king@mm.com"}
length: 2 __proto__: Array(0)
但是当我从下拉菜单中选择一个选项时,loginUserList
在控制台中显示以下内容。
users (2) [Array(2), Array(2)]
0: Array(2)
0: {loginid: 1, logintype: 2, firstname: "abc", familyname: "aaa", email: "aa@in.com"}
1: {loginid: 2, logintype: 1, firstname: "king", familyname: "abc", email: "king@mm.com"}
length: 2 __proto__: Array(0)
1: Array(2)
0: {loginid: 1, logintype: 2, firstname: "abc", familyname: "aaa", email: "aa@in.com"}
1: {loginid: 2, logintype: 1, firstname: "king", familyname: "abc", email: "king@mm.com"}
length: 2 __proto__: Array(0)
length: 2 __proto__: Array(0)
onchange
事件如下:
document.getElementById("selectLogin").addEventListener("change",function(){
console.log("users",loginUserList)
});
我无法弄清楚为什么会这样。请帮助