var drp1 = document.getElementById("drp1"); while(drp1.options.length>0) { drp1.options.remove(0); }
}
动态更改方法(根据城市代码取得该市商业区并添加到DropDownList中)
function getsyq() { var city = document.getElementById("DropDownList_Cities").value; //取得城市代码 var htp = new ActiveXObject("Msxml2.XMLHTTP"); var drp1 = document.getElementById("drp1"); var url = "?stat=1&city="+city htp.open("post",url,true) htp.onreadystatechange=function() { if(htp.readyState==4) {
clearitem(); //清除原有下拉项 var str = htp.responseText; var opt = str.split(','); var s = opt.length for(var j = 0;j<s;j++) { var newOption = document.createElement("OPTION"); //定义一个新的项 var ff = opt[j].split('|'); newOption.text = ff[1]; newOption.value = ff[1]; drp1.options.add(newOption); } } } htp.send()
}
JavaScript实现DropDownList(Select)三级联动无刷新
<script language=javascript> function CountryChange(){ countryid=document.getElementById("ddlContry").value; if(countryid==null||countryid==""){ alert("请选择所属国家"); CountryDel("ddlProvince");//清空DropDownList CountryDel("ddlCity");//清空DropDownList return false; } var countrystring=""; var posturl=location.protocol+"//"+location.hostname+"//soleweb//AjaxEnjine//AreaShow.aspx?AreaId="+countryid; countrystring=openUrl(posturl); if(countrystring=="-2"){//查询失败 alert("数据查询失败"); return false; } //分割并写入DropDownList
var stringarray=countrystring.split("|"); for(var i=0;i<stringarray.length;i++){//重写DropDownList //拆分内部数组 var optionarray=stringarray[i].split(","); var newoption=document.createElement("option"); newoption.text=optionarray[0]; newoption.value=optionarray[1]; document.getElementById("ddlProvince").options.add(newoption); } }
function CountryDel(AreaName){//清空DropDownList var countnum=document.getElementById(AreaName).options.length; for(var i=1;i<countnum;i++){//清空DropDownList document.getElementById(AreaName).remove(countnum-i); } }
function ProvinceChange(){ countryid=document.getElementById("ddlProvince").value; if(countryid==null||countryid==""){ alert("请选择所属省"); CountryDel("ddlCity");//清空DropDownList return false; } var countrystring=""; var posturl=location.protocol+"//"+location.hostname+"//soleweb//AjaxEnjine//AreaShow.aspx?AreaId="+countryid; countrystring=openUrl(posturl); if(countrystring=="-2"){//查询失败 alert("数据查询失败"); return false; } //分割并写入DropDownList
var stringarray=countrystring.split("|"); for(var i=0;i<stringarray.length;i++){//重写DropDownList //拆分内部数组 var optionarray=stringarray[i].split(","); var newoption=document.createElement("option"); newoption.text=optionarray[0]; newoption.value=optionarray[1]; document.getElementById("ddlCity").options.add(newoption); } }
function openUrl(url) { var objxml=new ActiveXObject("Microsoft.XMLHttp") objxml.open("GET",url,false); objxml.send(); retInfo=objxml.responseText; if (objxml.status=="200") { return retInfo; } else { return "-2"; } } </script>