连动下拉框

<script type="text/javascript" >
var http_request=false;  
var localPro;  
var localCity;  
var returnXML;  
var returnText;  
function initVar()  
{  
 localPro=document.getElementById("province");  
 localCity=document.getElementById("city");  
}  
function createXMLHttp()  
 {  
   if(window.XMLHttpRequest) { //Mozilla 浏览器  
   http_request = new XMLHttpRequest();  
   if (http_request.overrideMimeType) {//设置MiME类别  
    http_request.overrideMimeType('text/xml');  
   }  
   }  
   else if (window.ActiveXObject) { // IE浏览器  
   try {  
    http_request = new ActiveXObject("Msxml2.XMLHTTP");  
   } catch (e) {  
    try {  
     http_request = new ActiveXObject("Microsoft.XMLHTTP");  
    } catch (e) {}  
   }  
  }  
 }  
function sendRequest()  
{  
 initVar();  
 createXMLHttp();  
 http_request.onreadystatechange = processRequest;  
 http_request.open("GET","city.xml", true);  
 http_request.send(null);  
}  
function processRequest()  
{  
     if (http_request.readyState == 4)
     { // 判断对象状态  
                        if (http_request.status == 200)
                        { // 信息已经成功返回,开始处理信息  
                  document.getElementById("statusTxt").innerHTML="";  
                  setProvince();  
                            }
                            else
                            { //页面不正常  
                                alert("您所请求的页面有异常。");  
                            }  
        }
        else
        {//只要未读取完成  
    document.getElementById("statusTxt").innerHTML="正则读取数据中……";  
  }  
}  
function setProvince()  
{   
 returnText=http_request.responseText;  
 returnXML=http_request.responseXML;  
 var pro=returnXML.getElementsByTagName("title");  
 var citys=returnXML.getElementsByTagName("item");  
 var cityNodes=citys[0].getElementsByTagName("city");  
 enterValue(pro,localPro);  
 enterValue(cityNodes,localCity);  
 outputAll();  
}  
function loadCity()  
{  
 var index=localPro.selectedIndex;  
 var cityNode=returnXML.getElementsByTagName("item");  
 var curCity=cityNode[index];  
 var cityNodes=curCity.getElementsByTagName("city");  
 enterValue(cityNodes,localCity);  
 outputAll();  
}  
function enterValue(cell,place)  
{  
 clearPreValue(place);  
 for(i=0;i<cell.length;i++){  
  var obj= document.createElement("option");  
  var doc=document.createTextNode(cell[i].firstChild.nodeValue);  
  obj.appendChild(doc);  
  place.appendChild(obj);  
 }  
}  
function clearPreValue(pc)  
{  
 while(pc.hasChildNodes())  
 pc.removeChild(pc.childNodes[0]);  
}  
function outputAll ()  
{  
 var cellValue= localPro.options[localPro.selectedIndex].firstChild.nodeValue+localCity.options[localCity.selectedIndex].firstChild.nodeValue;  
 document.getElementById("district").value=cellValue;  

</script>

posted on 2008-04-26 09:35  leanco  阅读(163)  评论(0)    收藏  举报

导航