将let first=document.getElementById('first'); let second=document.getElementById('second'); let third=document.getElementById('third'); let xhr=new XMLHttpRequest(); let url='http://10.0.55.125:8081//AjaxDemo/SelectServlet?id='; let loadArea=function(id,areaSelect){ areaSelect.innerHTML="<option value='-1'>---请选择---</option>"; xhr.open('get',url+id,true); xhr.send(null); xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ let areas=JSON.parse(xhr.responseText); for(let area of areas){ let newOption=document.createElement('option'); newOption.value=area.id; newOption.innerText=area.name; areaSelect.appendChild(newOption); } } } } first.addEventListener('change',function(e){ loadArea(this.options[this.options.selectedIndex].value,second); },false); second.addEventListener('change',function(e){ loadArea(this.options[this.options.selectedIndex].value,third); },false); loadArea(0,first);转化为jquery ajax
时间: 2023-06-13 09:08:12 浏览: 118
TypeError document.getElementById(...) is null错误原因
```
let first=document.getElementById('first');
let second=document.getElementById('second');
let third=document.getElementById('third');
let url='http://10.0.55.125:8081//AjaxDemo/SelectServlet?id=';
let loadArea=function(id, areaSelect){
areaSelect.html("<option value='-1'>---请选择---</option>");
$.ajax({
url: url + id,
type: 'GET',
dataType: 'json',
success: function(areas){
for(let area of areas){
let newOption=document.createElement('option');
newOption.value=area.id;
newOption.innerText=area.name;
areaSelect.append(newOption);
}
}
});
};
first.addEventListener('change',function(e){
loadArea($(this).val(), $(second));
},false);
second.addEventListener('change',function(e){
loadArea($(this).val(), $(third));
},false);
loadArea(0, $(first));
```
注意,上述代码需要引入 jQuery 库才能运行。
阅读全文