﻿//创建一个变量并初始化
var http_request = false;

//创建XMLHttpRequest对象
////根据浏览器的不同,创建的方法也不同,但大致可分两类
function createXMLHttpReq()
{
  //开始初始化XMLHttpRequest对象
  if(window.XMLHttpRequest)
  {//Mozilla浏览器或者非Microsoft浏览器
    http_request = new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {//ie浏览器
    try
    {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {}
    }
  }
  if(!http_request)
  {
    window.alert("不能创建XMLHttpRequest对象实例!");
    return false;
  }
}


function chang(x)//1就是第一级触发的  2就是第二级触发的
{
    var a=document.getElementById("select"+x);
    var b=document.getElementById("select"+(x+1));
    var c=a.selectedIndex;
    var detailname="类别";

    if(c!=0)
    {//如果确实选择了东西
        b.options.length = 0;
        b.options[0] =new Option("请选择"+detailname, "-1");

        createXMLHttpReq();
        var url = 'Aspx/Class_GetInfo.aspx?ClassId='+a.options[c].value;
        http_request.open("GET",url,true);
        http_request.onreadystatechange = function() {
                    if(http_request.readyState==4)
                    {
                      if(http_request.status==200)
                      {//信息已经成功返回,开始处理信息
                         var backInfo = http_request.responseText;
		                 var arr = backInfo.split(",");
                         for(var i=0;i<arr.length;i++)
                         {
                             x=arr[i].split("|");
                             b.options[i+1] =new Option(x[0],x[1]);
                         }
                      }
                      else
                      {//页面不正常
                          alert("*您所请求的页面有异常!");
                      }
                  }  
        }
        
        http_request.send(null);       

        b.options[0].selected=true;
    }
}


//------------------------------------------------------------- 下面的是默认加载第一级的类别
function loadprovince()
{
  var a= document.getElementById("select1");
  a.options[0] =new Option("请选择类别", "-1");
  createXMLHttpReq();
  var url = "Aspx/Class_GetInfo.aspx?ClassId=1";
  send_requ(url);
} 


//初始化,指定处理函数,发送请求的函数
function send_requ(url)
{
  //确定发送请求的方式和url以及是否同步执行下段代码

  //第一个参数是指传递参数的方法(get,post) url是指服务端文件的地址 true有两个值是指是否同步
  http_request.open("GET",url,true);
  
  //指定处理请求的函数
  http_request.onreadystatechange = processReq;  
  //send方法里的参数如果是get发送的话可以为null
  http_request.send(null);
}

//处理返回信息的函数
function processReq() 
{
    if(http_request.readyState==4)
    {
        if(http_request.status==200)
        {//信息已经成功返回,开始处理信息
           var backInfo = http_request.responseText;
           var a= document.getElementById("select1");
           var arr = backInfo.split(",");
           for(var i=0;i<arr.length;i++)
           {   
               x=arr[i].split("|");
               a.options[i+1] =new Option(x[0],x[1]);
           }
        }
        else
        {//页面不正常
            alert("*您所请求的页面有异常!");
        }
    }
}