用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, time: new Date().getTime() }, 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         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值