基于Ajax實(shí)現(xiàn)下拉框聯(lián)動(dòng)顯示數(shù)據(jù)
公司做項(xiàng)目的時(shí)候,需要用到下拉框聯(lián)動(dòng)顯示數(shù)據(jù)的功能,索性利用Ajax來(lái)實(shí)現(xiàn),看到時(shí)間比較充裕,就沒(méi)去找demo自己去想方法寫(xiě)了。純自己的想法,有些可能比較弱智,希望不要見(jiàn)笑。
頁(yè)面中的兩個(gè)下拉列表框:
<tr>
<td style="width: 130px">
所在學(xué)院:</td>
<td style="width: 100px">
<select id="college" style="width: 200px" runat="server" onchange="changcollege(this.value)">
<option value="0">
--請(qǐng)選擇所在學(xué)院學(xué)院--
</option>
</select></td>
</tr>
<tr>
<td style="width: 130px">
所在專業(yè):</td>
<td style="width: 100px">
<select id="specialty" style="width: 200px" runat="server" onchange="SaveSpecical(this.value)">
<option value="0">
--請(qǐng)選擇所在專業(yè)--
</option>
</select></td>
</tr>
JS腳本代碼:
<script type="text/javascript">
var http_request = false;
function send_request(method,url,content,responseType,callback)//定義發(fā)送請(qǐng)求的函數(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對(duì)象失敗");
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ā)生改變時(shí)觸發(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é)果添加到下級(jí)的列表框中
{
var tmp=classList[i].split(",");
f.add(new Option(tmp[1],tmp[0]));
}
}
else
{
alert("您所請(qǐng)求的頁(yè)面有異常。");
}
}
}
</script>
我們將http請(qǐng)求發(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));//這個(gè)是從數(shù)據(jù)庫(kù)中根據(jù)傳來(lái)省的id 查詢出來(lái)的。學(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é)院的編號(hào)獲得相應(yīng)的專業(yè),并將專業(yè)的名稱用“|”分割組合成字符串返回給客戶端,客戶端腳本拆分字符串添加到下拉框中。

這里只是二級(jí)的聯(lián)動(dòng)顯示,三級(jí)聯(lián)動(dòng)數(shù)據(jù)的現(xiàn)實(shí)原理是一樣的。
本文的全部?jī)?nèi)容就到此結(jié)束了,希望大家學(xué)習(xí)Ajax實(shí)現(xiàn)下拉框聯(lián)動(dòng)顯示數(shù)據(jù)有所幫助。
- ajax三級(jí)聯(lián)動(dòng)下拉菜單效果
- AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)
- ThinkPHP使用心得分享-ThinkPHP + Ajax 實(shí)現(xiàn)2級(jí)聯(lián)動(dòng)下拉菜單
- JavaScript Ajax Json實(shí)現(xiàn)上下級(jí)下拉框聯(lián)動(dòng)效果實(shí)例代碼
- AJAX解析XML實(shí)例之下拉框省、市二級(jí)聯(lián)動(dòng)
- jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
- ajax讀取數(shù)據(jù)庫(kù)內(nèi)容實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)下拉選擇菜單示例
- Ajax實(shí)現(xiàn)無(wú)刷新三聯(lián)動(dòng)下拉框
- Jquery+ajax+JAVA(servlet)實(shí)現(xiàn)下拉菜單異步取值
- Ajax+Servlet實(shí)現(xiàn)無(wú)刷新下拉聯(lián)動(dòng)效果
相關(guān)文章
ajax 入門(mén)基礎(chǔ)之 XMLHttpRequest對(duì)象總結(jié)
在ajax中有一個(gè)最為核心的概念就是 XMLHttpRequest 對(duì)象,這篇文章將有助于我們更加深入的理解 ajax 的知識(shí)2009-08-08
AjaxFileUpload結(jié)合Struts2實(shí)現(xiàn)多文件上傳(動(dòng)態(tài)添加文件上傳框)
本文是腳本之家小編給大家分享的AjaxFileUpload結(jié)合Struts2實(shí)現(xiàn)多文件上傳功能,如果項(xiàng)目需求是不確定多少個(gè)文件,我們需要?jiǎng)討B(tài)的添加文件上傳框,具體實(shí)現(xiàn)代碼大家參考下本文2017-09-09
ajax實(shí)現(xiàn)點(diǎn)擊不同的鏈接讓返回的內(nèi)容顯示在特定div里
過(guò)ajax實(shí)現(xiàn)在一個(gè)web頁(yè)面點(diǎn)擊不同的鏈接,然后將返回的結(jié)果顯示在該頁(yè)面固定的div里2014-06-06
PPJOKE 0.1 (網(wǎng)頁(yè)嵌入聊天)提供下載
PPJOKE 0.1 (網(wǎng)頁(yè)嵌入聊天)提供下載...2006-10-10
Ajax技術(shù)(WEB無(wú)刷新提交數(shù)據(jù))-
Ajax技術(shù)(WEB無(wú)刷新提交數(shù)據(jù))-...2006-07-07
ajax的 IE cache 相關(guān)問(wèn)題解決
運(yùn)用Ajax做了一個(gè)名字檢驗(yàn),第一次是有效的,但是提交過(guò)后,再檢驗(yàn)一次,結(jié)果就不對(duì)了,是由于IE的cache的原因,接下來(lái)介紹下詳細(xì)的解決方法,感性的朋友可以參考下哈2013-04-04
利用Ajax實(shí)現(xiàn)智能回答的機(jī)器人示例代碼
這篇文章主要介紹了利用Ajax實(shí)現(xiàn)智能回答的機(jī)器人,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

