我做了一个javascript事件监听器,但是我不确定是否没有违反任何内容。这是我的代码的样子:
const url = window.location;
document.getElementById('save-employee-allowance').addEventListener('click', (e) =>{
let allowance = document.getElementById('allowance').addEventListener('change', (e) =>{
let item = document.getElementById('allowance').value;
if(item){
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', url.origin+"/api/allowance/search/1/"+item);
httpRequest.send();
httpRequest.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
if(typeof(JSON.parse(httpRequest.responseText).id) !== 'undefined'){
return JSON.parse(httpRequest.responseText);
}
}
}
}
});
console.log('Allowance Val >>> '+allowance);
});
并且在运行时,它为我的余量变量提供了一个未定义的值。 我该怎么做才能返回并获取变更侦听器的价值?