基于JQuery的Select下拉框下拉框联动(级联)

这段时间在指导学生完成实训项目,由一个用到了JQuery进行下拉框(select)联动(添加删除option)的操作,本来在上课中都是讲过的,但时间一长都忘光了,下面把这段简单的JS贴出来,给入门者一个DEMO吧,以后有学生不会写的时候他能到这找到参考。

代码要点:

1、使用JQuery给select标签添加option元素时,直接使用:

Js代码   收藏代码
  1. $("筛选符").append("<option value='隐藏值'>显示文字</option>")  

2、清空select中所有元素可以使用:

Js代码   收藏代码
  1. $(".child").html("")  

    或者是

Java代码   收藏代码
  1. $(".child").empty()  

3、获取select标签选择值时用:(直调用val()方法即可)

Java代码   收藏代码
  1. $(".parent").val()  

下面是示例JSP页面全文:

Js代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <meta http-equiv="pragma" content="no-cache"/>  
  8. <meta http-equiv="cache-control" content="no-cache"/>  
  9. <meta http-equiv="expires" content="0"/>  
  10. <title>基于JQuery的下拉框级联操作</title>  
  11. <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>  
  12. <script type="text/javascript">  
  13. function changChild(tid){  
  14.     $.post("childSelect",{"tid":tid},function(json){  
  15.         $(".child").html("");//清空学生下拉框  
  16.         for(var i=0;i<json.length;i++){  
  17.             //添加一个学生  
  18.             $(".child").append("<option value='"+json[i].id+"'>"+json[i].name+"</option>");  
  19.         }  
  20.     },'json');  
  21. }  
  22. $(function(){  
  23.     //初始化教师下拉框  
  24.     $.post("parentSelect",null,function(json){  
  25.         for(var i=0;i<json.length;i++){  
  26.             //添加一个教师  
  27.             $(".parent").append("<option value='"+json[i].id+"'>"+json[i].name+"</option>");  
  28.         }  
  29.         changChild($(".parent").val());  
  30.     },'json');  
  31.     //注册教师下拉框事件  
  32.     $(".parent").change(function(){  
  33.         changChild($(this).val());  
  34.     });  
  35. });  
  36. </script>  
  37. </head>  
  38. <body>  
  39. <h3>使用JQuery进行下拉框的联动</h3>  
  40. <p>  
  41.     改变教师下拉框,发送AJAX请求,根据返回的JSON数据重新填充学生下拉框。  
  42. </p>  
  43. <hr/>  
  44. <select class="parent"></select>  
  45. <select class="child"></select>  
  46. </body>  
  47. </html>  

服务端是用Struts的一个Action,使用Json-lib将数据转化成json字符串:(见全文)

Java代码   收藏代码
  1. public class SelectChangeAction {  
  2.     private static List<Teacher> teachers = new ArrayList<Teacher>();  
  3.     private static List<Student> students = new ArrayList<Student>();  
  4.     private int tid;  
  5.     static{  
  6.         Teacher teacher = null;   
  7.         Student student = null;  
  8.         for(int i=0;i<10;i++){  
  9.             teacher = new Teacher();  
  10.             teacher.setId(i);  
  11.             teacher.setName("教师【"+i+"】");  
  12.             for(int j=0;j<10;j++){  
  13.                 student = new Student();  
  14.                 student.setId(j);  
  15.                 student.setName(teacher.getName()+"的学生【"+i+"|"+j+"】");  
  16.                 student.setTeacher(teacher);  
  17.                 students.add(student);  
  18.             }  
  19.             teachers.add(teacher);  
  20.         }  
  21.     }  
  22.       
  23.     /** 
  24.      * 输出教师信息 
  25.      * @return 
  26.      */  
  27.     public String parent(){  
  28.         String json = JSONArray.fromObject(teachers).toString();  
  29.         HttpServletResponse response = ServletActionContext.getResponse();  
  30.         response.setCharacterEncoding("UTF-8");  
  31.         try {  
  32.             response.getWriter().write(json);  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return null;  
  37.     }  
  38.     /** 
  39.      * 输出学生信息 
  40.      * @return 
  41.      */  
  42.     public String child(){  
  43.         List<Student> list = new ArrayList<Student>();  
  44.         for (Student student : students) {  
  45.             if(student.getTeacher().getId() == tid){  
  46.                 list.add(student);  
  47.             }  
  48.         }  
  49.         String json = JSONArray.fromObject(list).toString();  
  50.         HttpServletResponse response = ServletActionContext.getResponse();  
  51.         response.setCharacterEncoding("UTF-8");  
  52.         try {  
  53.             response.getWriter().write(json);  
  54.         } catch (IOException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.         return null;  
  58.     }  
  59.     public int getTid() {  
  60.         return tid;  
  61.     }  
  62.     public void setTid(int tid) {  
  63.         this.tid = tid;  
  64.     }  

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值