溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ajax如何實現(xiàn)下拉框聯(lián)動顯示數(shù)據(jù)

發(fā)布時間:2021-06-25 10:59:56 來源:億速云 閱讀:152 作者:小新 欄目:web開發(fā)

小編給大家分享一下Ajax如何實現(xiàn)下拉框聯(lián)動顯示數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

頁面中的兩個下拉列表框:

<tr>
        <td >
          所在學(xué)院:</td>
        <td >
          <select id="college"  runat="server" onchange="changcollege(this.value)">
            <option value="0">
              --請選擇所在學(xué)院學(xué)院--
            </option>
          </select></td>
      </tr>
      <tr>
        <td >
          所在專業(yè):</td>
        <td >
          <select id="specialty"  runat="server" onchange="SaveSpecical(this.value)">
            <option value="0">
              --請選擇所在專業(yè)--
            </option>
          </select></td>
      </tr>

JS腳本代碼:

<script type="text/javascript">
var http_request = false;
function send_request(method,url,content,responseType,callback)//定義發(fā)送請求的函數(shù)
{
  http_request=false;
  if(window.XMLHttpRequest)
  {
    http_request=new XMLHttpRequest();
    if(http_request.overrideMimeType)
    {
      http_request.overrideMimeType("text/xml");
    }
  }
  else
  {
    try
    {
      http_request=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        http_request=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {}
    }
  }
  if(!http_request)
  {
    window.alert("創(chuàng)建XMLHttpRequest對象失敗");
    return false;
  }
  if(responseType.toLowerCase()=="text")
  {
    http_request.onreadystatechange=callback;
  }
  else
  {
    window.alert("ERR");
    return false;
  }
  if(method.toLowerCase()=="get")
  {
    http_request.open(method,url,true);
  }
  else if(method.toLowerCase()=="post")
  {
    http_request.open(method,url,true);
    http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  }
  else
  {
    window.alert("Err");
    return false;
  }
  http_request.send(content);
}
function changcollege(va)//當(dāng)學(xué)院下拉列表發(fā)生改變時觸發(fā)的腳本事件
{
  if(va!='0')
  {
    var speciality = document.getElementById("specialty");
    speciality.disabled=false;
    var url="Handler.ashx?type=college&id="+va;
    send_request("GET",url,null,"text",populateClass3);
  }
}
function populateClass3()//Ajax執(zhí)行成功的回調(diào)函數(shù)
{
  var f=document.getElementById("specialty");
  if(http_request.readyState==4)
  {
      if(http_request.status==200)
      {
        var list=http_request.responseText;
        var classList=list.split("|");
        f.options.length=1;
        for(var i=0;i<classList.length;i++)
            //將取得的結(jié)果添加到下級的列表框中
        {
          var tmp=classList[i].split(",");
          f.add(new Option(tmp[1],tmp[0]));
        }
      }
      else
      {
        alert("您所請求的頁面有異常。");
      }
  }
}  
</script>

我們將http請求發(fā)送給服務(wù)端的Handler.ashx進(jìn)行處理。

public class Handler : IHttpHandler 
{
  public void ProcessRequest(HttpContext context)
  {
    string type = context.Request.QueryString["type"];
    if (type.Equals("college"))
    {
      string id = context.Request.QueryString["id"];
      context.Response.ContentType = "text/plain";
      context.Response.Write(getSpecialty(id));//這個是從數(shù)據(jù)庫中根據(jù)傳來省的id 查詢出來的。學(xué)院的名字和主鍵,主鍵以便去查專業(yè)的名字
    }
  }
  public string getSpecialty(string college)
  {
    DataSet ds = GetInformation.GetSpecialtyInfo(college);
    string str = "";
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      if (i == ds.Tables[0].Rows.Count - 1)
      {
        str += ds.Tables[0].Rows[i]["SpecialtyID"].ToString() + "," + ds.Tables[0].Rows[i]["SpecialtyName"].ToString();
      }
      else
      {
        str += ds.Tables[0].Rows[i]["SpecialtyID"].ToString() + "," + ds.Tables[0].Rows[i]["SpecialtyName"].ToString() + "|";
      }
    }
    return str.Trim();
  }
  public bool IsReusable {
    get {
      return false;
    }
  }
}

根據(jù)學(xué)院的編號獲得相應(yīng)的專業(yè),并將專業(yè)的名稱用“|”分割組合成字符串返回給客戶端,客戶端腳本拆分字符串添加到下拉框中。

Ajax如何實現(xiàn)下拉框聯(lián)動顯示數(shù)據(jù)

這里只是二級的聯(lián)動顯示,三級聯(lián)動數(shù)據(jù)的現(xiàn)實原理是一樣的。

以上是“Ajax如何實現(xiàn)下拉框聯(lián)動顯示數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI