原創:ajax實現省市區三級連動

來源:互聯網
上載者:User

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>My First Ajax</TITLE>
</HEAD>

<script>
   var xmlHttp;
   var ProID;
   var CityID;
   var ProNode;
   var CityNode;
   function createXMLHttpRequest()
        {
    
           if(window.ActiveXObject)
             {
     xmlHttp=new window.ActiveXObject("Microsoft.XMLHttp");
       }
           else if(window.XMLHttpRequest)
       {
      xmlHttp=new window.XMLHttpRequest();
    }
 
  }

   function ProLoad()
    {

   createXMLHttpRequest();
   xmlHttp.OnReadyStateChange=HandleStateChange;
   xmlHttp.open("Get","area.xml",true);
   xmlHttp.send(null);
  
    }

   function CityLoad()
    {
   ProID=document.ajax.ProSelect.value;
   //alert(ProID);
         if (!xmlHttp)
         {
        xmlHttp.abort();
         }
   createXMLHttpRequest();
   xmlHttp.OnReadyStateChange=HandleStateChange2;
   xmlHttp.open("Get","area.xml",true);
   xmlHttp.send(null);
  
    }

   function AreaLoad()
    {
   CityID=document.ajax.CitySelect.value;
   //alert(CityID);
  
         if (!xmlHttp)
         {
        xmlHttp.abort();
         }
   createXMLHttpRequest();
   xmlHttp.OnReadyStateChange=HandleStateChange3;
   xmlHttp.open("Get","area.xml",true);
   xmlHttp.send(null);
  
    }
 

 function HandleStateChange()
  {
     if(xmlHttp.ReadyState==4)
     {
     if(xmlHttp.Status==200)
     {
        try
        {
           pro();
        }
        catch (exception)
        {
         alert("error");
        }
       
     }
     }

  }

   function HandleStateChange2()
  {
     if(xmlHttp.ReadyState==4)
     {
     if(xmlHttp.Status==200)
     {
        try
        {
           ProChagnge();
        }
        catch (exception)
        {
         alert("error");
        }
       
     }
     }

  }

     function HandleStateChange3()
  {
     if(xmlHttp.ReadyState==4)
     {
     if(xmlHttp.Status==200)
     {
        try
        {

            CityChagnge();
       //alert(CityID);
        }
        catch (exception)
        {
         alert("error");
        }
       
     }
     }

  }

 function pro()
 {

      var select = document.getElementById("ProSelect");
   while (select.length > 0)
    {
             select.remove(0);
    }
     document.ajax.ProSelect.add(document.createElement("OPTION"));
  document.ajax.ProSelect.options[0].text="請選擇省份";
  document.ajax.ProSelect.options[0].value="請選擇省份";
     var xmlDoc=xmlHttp.responseXML;
     var proNode=xmlDoc.getElementsByTagName("province");
  for(i=0;i<proNode.length;i++)
  { 
     //alert(proNode[i].getAttribute("name"))
  document.ajax.ProSelect.add(document.createElement("OPTION"));
  document.ajax.ProSelect.options[i+1].text=proNode[i].getAttribute("name");
  document.ajax.ProSelect.options[i+1].value=proNode[i].getAttribute("id");
  }

 }

  function ProChagnge()
 {
      var select = document.getElementById("CitySelect");
   while (select.length > 0)
    {
             select.remove(0);
    }
     document.ajax.CitySelect.add(document.createElement("OPTION"));
  document.ajax.CitySelect.options[0].text="請選擇城市";
  document.ajax.CitySelect.options[0].value="no";

     var select = document.getElementById("AreaSelect");
  while (select.length > 0)
    {
             select.remove(0);
    }
   document.ajax.AreaSelect.add(document.createElement("OPTION"));
   document.ajax.AreaSelect.options[0].text="請選擇";
   document.ajax.AreaSelect.options[0].value="no";

  var xmlDoc=xmlHttp.responseXML;
     var proNode=xmlDoc.getElementsByTagName("province");

  for(i=0;i<proNode.length;i++)
  { 
          if (proNode[i].getAttribute("id")==ProID)
          {
          var proNode2=xmlDoc.getElementsByTagName("province")[i];
    break;
          }
  }
    CityNode=proNode2.getElementsByTagName("city");

 for(i=0;i<CityNode.length;i++)
  { 

     //alert(proNode[i].getAttribute("name"))
  if(CityNode[i].getAttribute("id")!="")
  {
   document.ajax.CitySelect.add(document.createElement("OPTION"));
   document.ajax.CitySelect.options[i+1].text=CityNode[i].getAttribute("name");
   document.ajax.CitySelect.options[i+1].value=CityNode[i].getAttribute("id");
  }
  }

   
 }

  function CityChagnge()
 {

     var select = document.getElementById("AreaSelect");
  while (select.length > 0)
     {
     select.remove(0);
     }
     if(document.ajax.CitySelect.value!="no") 
     {
   for(i=0;i<CityNode.length;i++)
   { 
     if (CityNode[i].getAttribute("id")==CityID)
     {
     //alert(i);
     var CityNode2=CityNode[i];
     break;
     }
   }
  

 

  var AreaNode=CityNode2.getElementsByTagName("area");
  for(i=0;i<AreaNode.length;i++)
   { 
   if(AreaNode[i].getAttribute("id")!="")
   {
    document.ajax.AreaSelect.add(document.createElement("OPTION"));
    document.ajax.AreaSelect.options[i].text=AreaNode[i].childNodes[0].nodeValue;
    document.ajax.AreaSelect.options[i].value=AreaNode[i].getAttribute("id");
   }
   }
  
     } 
  else
  {
    document.ajax.AreaSelect.add(document.createElement("OPTION"));
   document.ajax.AreaSelect.options[0].text="請選擇";
   document.ajax.AreaSelect.options[0].value="no";
  }
 }

</script>
<body onload="ProLoad()">
<form action=# name="ajax" >
<select name="ProSelect" onchange="CityLoad()">
<option>請選擇省份</option>
</select>

<select name="CitySelect" onchange="AreaLoad()">
<option>請選擇城市</option>
</select>
<select name="AreaSelect">
<option>請選擇</option>
</select>
</form>
</BODY>
</HTML>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.