加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax提高篇(7)Ajax实现简单的下拉框联动显示数据

发布时间:2020-12-16 01:52:25 所属栏目:百科 来源:网络整理
导读:页面中的两个下拉列表框: [html] view plain copy tr td style = "width:130px" 所在学院: / td td style = "width:100px" select id = "college" style = "width:200px" runat = "server" onchange = "changcollege(this.value)" option value = "0" --请

页面中的两个下拉列表框:

[html] view plain copy
  1. <tr>
  2. tdstyle="width:130px" 所在学院:</tdtdstyle="width:100px"selectid="college"style="width:200px"runat="server"onchange="changcollege(this.value)"optionvalue="0" --请选择所在学院学院--
  3. optionselect> 所在专业:selectid="specialty"style="width:200px"runat="server"onchange="SaveSpecical(this.value)" --请选择所在专业--
  4. >


JS脚本代码

[javascript] copy
    <scripttype="text/javascript">
  1. varhttp_request=false;
  2. functionsend_request(method,url,content,responseType,callback)//定义发送请求的函数
  3. {
  4. http_request=false;
  5. if(window.XMLHttpRequest)
  6. {
  7. http_request=newXMLHttpRequest();
  8. if(http_request.overrideMimeType)
  9. http_request.overrideMimeType("text/xml");
  10. }
  11. }
  12. else
  13. try
  14. newActiveXObject("Msxml2.XMLHTTP");
  15. catch(e)
  16. newActiveXObject("Microsoft.XMLHTTP");
  17. {}
  18. if(!http_request)
  19. window.alert("创建XMLHttpRequest对象失败");
  20. returnif(responseType.toLowerCase()=="text")
  21. http_request.onreadystatechange=callback;
  22. else
  23. window.alert("ERR");
  24. if(method.toLowerCase()=="get")
  25. http_request.open(method,true);
  26. elseif(method.toLowerCase()=="post")
  27. http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  28. window.alert("Err");
  29. http_request.send(content);
  30. functionchangcollege(va)//当学院下拉列表发生改变时触发的脚本事件
  31. if(va!='0')
  32. varspeciality=document.getElementById("specialty");
  33. speciality.disabled=varurl="Handler.ashx?type=college&id="+va;
  34. send_request("GET",null,"text",populateClass3);
  35. functionpopulateClass3()//Ajax执行成功的回调函数
  36. varf=document.getElementById("specialty");
  37. if(http_request.readyState==4)
  38. if(http_request.status==200)
  39. varlist=http_request.responseText;
  40. varclassList=list.split("|");
  41. f.options.length=1;
  42. for(vari=0;i<classList.length;i++)
  43. //将取得的结果添加到下级的列表框中
  44. vartmp=classList[i].split(",");
  45. f.add(newOption(tmp[1],tmp[0]));
  46. alert("您所请求的页面有异常。");
  47. </script>

我们将http请求发送给服务端的Handler.ashx进行处理

copy
    publicclassHandler:IHttpHandler
  1. voidProcessRequest(HttpContextcontext)
  2. stringtype=context.Request.QueryString["type"];
  3. if(type.Equals("college"))
  4. stringid=context.Request.QueryString["id"];
  5. context.Response.ContentType="text/plain";
  6. context.Response.Write(getSpecialty(id));//这个是从数据库中根据传来省的id查询出来的。学院的名字和主键,主键以便去查专业的名字
  7. publicstringgetSpecialty(stringcollege)
  8. DataSetds=GetInformation.GetSpecialtyInfo(college);
  9. stringstr="";
  10. inti=0;i<ds.Tables[0].Rows.Count;i++)
  11. if(i==ds.Tables[0].Rows.Count-1)
  12. str+=ds.Tables[0].Rows[i]["SpecialtyID"].ToString()+","+ds.Tables[0].Rows[i]["SpecialtyName"].ToString();
  13. +ds.Tables[0].Rows[i]["SpecialtyName"].ToString()+"|";
  14. returnstr.Trim();
  15. publicboolIsReusable{
  16. get{
  17. }

根据学院的编号获得相应的专业,并将专业的名称用“|”分割组合成字符串返回给客户端,客户端脚本拆分字符串添加到下拉框中。

这里只是二级的联动显示,三级联动数据的现实原理是一样的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读