前端之家收集整理的这篇文章主要介绍了
用AJAX做下拉框级联,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<script language="javascript"type="text/javascript">
2 $(document).ready(function () {
3 var gettime = function () {
4 var dt = this.options[this.selectedIndex].value;//获取当前触发Change事件select控件当前选中项的值
5 $.get("./FTimeList",{ date: dt,time: new Date().getTime() },function (data) {//将选中值传入,作为查询条件
6 var time = data.getElementsByTagName("time");//以<time></time>为标签去子元素,list
7 var hl = "";
8 for (var i = 0; i < time.length; i++) {
9 hl += "<option value=\"" + time[i].firstChild.data + "\" >" + time[i].firstChild.data + "</option>";
10 //遍历time赋值给h1(此处做成<option>标签样式)
11 }
12 $("#DroTime").html(hl); //赋值给ID为DroTime的dropdownlist实现绑定
13 });
14 }
15 $("#DroDate").change(gettime);//事件绑定是ID为DroDate的下拉框触发事件,然后将你选中的值传到后台,然后根据这个值查出数据返回绑定 给标签为DroTime的下拉框 16 var getdate = function () { //和上面差不多,是下一级的级联选项,如果不需要可以不用 17 dc = this.options[this.selectedIndex].value;
18 $.get("./STeaTime",{ TID: dc,function (data) {
19 var date = data.getElementsByTagName("date");
20 var hl = "";
21 for (var i = 0; i < date.length; i++) {
22 hl += "<option>" + date[i].firstChild.data + "</option>";
23 }
24 $("#DroDate").html(hl);
25 gettime;
26 });
27 }
28 $("#DroTec").change(getdate);
29 });
30 </script>
以下是后台Action代码:
1 public ActionResult FTimeList(string date)
2 {
3 string strr="<?xml version=\"1.0\"?><node>";//加XML文件头
4 if (TeacherID != null)
5 {
6 var TeacherName = from s4 in wtDB.View_TeacherName
7 where s4.TeacherID == TeacherID
8 select s4;
9 if (TeacherName.Count() > 0)
10 {
11 12 var TeacherTime = from s4 in wtDB.WT_AvailableTime
13 where s4.CustomerId == TeacherName.First().Id && s4.Date ==Convert.ToDateTime(date)//用户ID
14 select new { s4.StartTimeId,s4.Date };
15 List<SelectListItem> TimeList = new List<SelectListItem>();// 老师有空时间
16 List<SelectListItem> DateList = new List<SelectListItem>();//日期
17 if (TeacherTime.Count() > 0)
18 {
19 foreach (var time in TeacherTime)
20 {
21 var timeinfo = from s5 in wtDB.WT_Hours
22 where s5.Id == time.StartTimeId
23 select s5;//前面的都可以忽略,我功能上要用到的查询语句 24 foreach (var time2 in timeinfo)
25 {
26 strr += "<time>" + time2.Time + "</time>";//构造返回的数据,以<time>为节点,前台也以<time>为节点取值
27 }
28
29
30 }
31 }
32 }
33 }
34 strr += "</node>";//结尾
35
36 return Content(strr,"text/xml");
37 }