1. 程式人生 > >基於JQuery的Select下拉框下拉框聯動(級聯)

基於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. publicclass SelectChangeAction {  
  2.     privatestatic List<Teacher> teachers = new ArrayList<Teacher>();  
  3.     privatestatic List<Student> students = new ArrayList<Student>();  
  4.     privateint 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.      * @return 
  25.      */
  26.     public String parent(){  
  27.         String json = JSONArray.fromObject(teachers).toString();  
  28.         HttpServletResponse response = ServletActionContext.getResponse();  
  29.         response.setCharacterEncoding("UTF-8");  
  30.         try {  
  31.             response.getWriter().write(json);  
  32.         } catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         returnnull;  
  36.     }  
  37.     /** 
  38.      * 輸出學生資訊 
  39.      * @return 
  40.      */
  41.     public String child(){  
  42.         List<Student> list = new ArrayList<Student>();  
  43.         for (Student student : students) {  
  44.             if(student.getTeacher().getId() == tid){  
  45.                 list.add(student);  
  46.             }  
  47.         }  
  48.         String json = JSONArray.fromObject(list).toString();  
  49.         HttpServletResponse response = ServletActionContext.getResponse();  
  50.         response.setCharacterEncoding("UTF-8");  
  51.         try {  
  52.             response.getWriter().write(json);  
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.         returnnull;  
  57.     }  
  58.     publicint getTid() {  
  59.         return tid;  
  60.     }  
  61.     publicvoid setTid(int tid) {  
  62.         this.tid = tid;  
  63.     }