js省市縣三級聯動下拉列表的示例
阿新 • • 發佈:2019-01-03
function selectOnChange(SelId,toSelId){
//document.getElementById(toSelId).innerHTML="";
$.ajax({
url:"<%=basePath%>medium/findAreaByParentId.do",
type:"post",
cache: false,
data:"PARENTID="+SelId,
dataType:"json",
success : function(data){
createSelectObj(data.dataList,toSelId);
if(toSelId == "city"){ //當是省級下拉列表呼叫這個方法時 需要再模仿市級下拉列表呼叫一次
var val = document.getElementById(toSelId);
var index = val.selectedIndex;
var aa = val.options[index].value;
//var aa = val.options[val.options.selectedIndex].value;
selectOnChange(aa,"county");
}
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
}
function createSelectObj(arr,toSelId) {
if (arr != null && arr.length > 0) {
var obj = document.getElementById(toSelId);
obj.innerHTML = "";
for ( var o in arr) {
var op = document.createElement("option");
op.setAttribute("value", arr[o].ID);
//op.text=arr[o].name;//這一句在ie下不起作用,用下面這一句或者innerHTML
op.appendChild(document.createTextNode(arr[o].AREANAME));
obj.appendChild(op);
}
}
else{
var obj = document.getElementById(toSelId);
obj.innerHTML = "";
var op = document.createElement("option");
op.setAttribute("value", "");
op.appendChild(document.createTextNode("暫無"));
obj.appendChild(op);
}
}
//document.getElementById(toSelId).innerHTML="";
$.ajax({
url:"<%=basePath%>medium/findAreaByParentId.do",
type:"post",
cache: false,
data:"PARENTID="+SelId,
dataType:"json",
success : function(data){
createSelectObj(data.dataList,toSelId);
if(toSelId == "city"){ //當是省級下拉列表呼叫這個方法時 需要再模仿市級下拉列表呼叫一次
var val = document.getElementById(toSelId);
var index = val.selectedIndex;
var aa = val.options[index].value;
//var aa = val.options[val.options.selectedIndex].value;
selectOnChange(aa,"county");
}
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
}
function createSelectObj(arr,toSelId) {
if (arr != null && arr.length > 0) {
var obj = document.getElementById(toSelId);
obj.innerHTML = "";
for ( var o in arr) {
var op = document.createElement("option");
op.setAttribute("value", arr[o].ID);
//op.text=arr[o].name;//這一句在ie下不起作用,用下面這一句或者innerHTML
op.appendChild(document.createTextNode(arr[o].AREANAME));
obj.appendChild(op);
}
}
else{
var obj = document.getElementById(toSelId);
obj.innerHTML = "";
var op = document.createElement("option");
op.setAttribute("value", "");
op.appendChild(document.createTextNode("暫無"));
obj.appendChild(op);
}
}