校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > JSP頁面中如何用select標簽實現級聯

JSP頁面中如何用select標簽實現級聯

熱門標簽:商洛電銷 北票市地圖標注 高德地圖標注樣式 杭州ai語音電銷機器人功能 電銷機器人好賣么 四川保險智能外呼系統商家 地圖標注線上教程 電銷機器人是有一些什么技術 杭州語音電銷機器人軟件

做查詢頁面,查詢條件比較多的時候往往會涉及到級聯。舉個簡單的例子,拿教務系統來說,我們要查詢教學計劃信息,查詢條件是入學批次、學生層次(專升本、高升專)、專業、課程。

它們之間有什么級聯關系呢?入學批次影響學生層次(某個入學批次可能只有專升本或者高升專一個學生層次)、專業、課程,學生層次影響專業、課程,專業又影響課程。也就是說當選擇入學批次時,學生層次、專業和課程的下拉框會局部刷新,選擇學生層次時,專業和課程的下拉框會局部刷新,選擇專業時,課程的下拉框也會局部刷新。

我們當然不希望已經選擇的操作隨著頁面的刷新又被初始化,再者前面提到選擇一項后相關的下拉框是局部刷新。很容易想到用填充頁面的方法來實現級聯。

筆者的填充方法是通過提交JS,由Controller獲取數據,將數據傳到輔助的JSP頁面,再用回調函數將輔助JSP頁面中的數據填充給相應下拉框。說的抽象,直接上代碼了,四級級聯稍微麻煩一些,知道原理后也好做,筆者上三級級聯的代碼。級聯樣式如下圖:



 JSP頁面代碼:

復制代碼 代碼如下:

   table>
    tr>
     td width="400px" align="left">入學批次:SELECT NAME="grade"
      id="grade" onchange="refreshEduLevelAndSpecialAjax();">  //選擇入學批次會刷新層次和專業
       OPTION VALUE="0">
        --請選擇--
        c:forEach items="${gradeInfo}" var="gradeInfo">
         OPTION VALUE="${gradeInfo.gradeName}">${gradeInfo.gradeName}        
        /c:forEach>
     /SELECT>/td>
     td width="400px" align="left">統考課程:SELECT
      NAME="uniExamCourseId" id="uniExamCourseId">
       OPTION VALUE="0">
        --請選擇--
        c:forEach items="${unifiedExamCourseList}" var="uniExamCourse">
         OPTION VALUE="${uniExamCourse.id}">${uniExamCourse.uniExamCourseName}        
        /c:forEach>
     /SELECT>/td>
    /tr>
    tr>
     td colspan="2" id="refreshEduLevelAndSpecialAjax">    //設置ID,用于填充層次和專業的下拉框
      table>
       tr>
        td width="400" align="left">層nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;次:SELECT
         NAME="eduLevelId" id="eduLevelId"
         onchange="refreshSpecialAjax();">    //選擇層次后刷新專業
          OPTION VALUE="0">--請選擇--/OPTION>
          c:forEach items="${educationLevel}" var="educationLevel">
           OPTION VALUE="${educationLevel.id}">${educationLevel.educationLevelName}          
          /c:forEach>
        /SELECT>/td>
        td width="400" align="left" id="refreshSpecialAjax">專nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;業:SELECT            //設置ID,用于填充專業的下拉框
         NAME="specialId" id="specialId">
          OPTION VALUE="0">--請選擇--/OPTION>
          c:forEach items="${specialList}" var="special">
           OPTION VALUE="${special.id}">${special.specialName}          
          /c:forEach>
        /SELECT>/td>
       /tr>
      /table>
     /td>
    /tr>
   /table>

JS的代碼如下:
復制代碼 代碼如下:

//JavaScript Document
 var xmlHttp; //用于保存XMLHttpRequest對象的全局變量
 //用于創建XMLHttpRequest對象
 function createXmlHttp() {
  //根據window.XMLHttpRequest對象是否存在使用不同的創建方式
  if (window.XMLHttpRequest) {
   xmlHttp = new XMLHttpRequest(); //FireFox、Opera等瀏覽器支持的創建方式
  } else {
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器支持的創建方式
  }
 }
 function refreshEduLevelAndSpecialAjax() {
  var grade = document.getElementById("grade").value;
  refreshEduLevelAndSpecial(grade);
 }
 function refreshEduLevelAndSpecial(grade) {
  createXmlHttp(); //創建XMLHttpRequest對象
  xmlHttp.onreadystatechange = refreshEduLevelAndSpecialElement; //設置回調函數
  xmlHttp.open("POST", "eduLevelAndSpecialByGradeNameInSpecialDetail",
    true); //發送POST請求
  xmlHttp.setRequestHeader("Content-type",
    "application/x-www-form-urlencoded");
  xmlHttp.send("grade=" + grade);
 }
 //處理服務器返回的信息 更新層次專業下拉框
 function refreshEduLevelAndSpecialElement() {
  if (xmlHttp.readyState == 4) {
   if (xmlHttp.status == 200) {
    //此處xmlHttp.responseText是請求的*Controller的某個方法返回的渲染頁面的源代碼
    document.getElementById("refreshEduLevelAndSpecialAjax").innerHTML = xmlHttp.responseText;
   }
  }
 }
 function refreshSpecialAjax() {
  var grade = document.getElementById("grade").value;
  var eduLevelId = document.getElementById("eduLevelId").value;
  refreshSpecial(grade, eduLevelId);
 }
 function refreshSpecial(grade, eduLevelId) {
  createXmlHttp(); //創建XMLHttpRequest對象
  xmlHttp.onreadystatechange = refreshSpecialElement; //設置回調函數
  xmlHttp.open("POST", "specialByGradeNameAndEduLevelIdInSpecialDetail",
    true); //發送POST請求
  xmlHttp.setRequestHeader("Content-type",
    "application/x-www-form-urlencoded");
  xmlHttp.send("grade=" + grade + "eduLevelId=" + eduLevelId);
 }
 //處理服務器返回的信息 更新專業下拉框
 function refreshSpecialElement() {
  if (xmlHttp.readyState == 4) {
   if (xmlHttp.status == 200) {
    //此處xmlHttp.responseText是請求的*Controller的某個方法返回的渲染頁面的源代碼
    document.getElementById("refreshSpecialAjax").innerHTML = xmlHttp.responseText;
   }
  }
 }

Controller代碼:
復制代碼 代碼如下:

@RequestMapping(value = "/eduLevelAndSpecialByGradeNameInSpecialDetail")
  public ModelAndView getEduLevelAndSpecialByGradeNameInSpecialDetail(HttpServletRequest request,
    HttpServletResponse response) throws JsonParseException, JsonMappingException, JSONException, IOException{  
   String gradeName=request.getParameter("grade");    
   String eduLevelId=request.getParameter("eduLevelId");  
   if(gradeName==null||gradeName.equals("0")){   
    gradeName="null";
   }
   if(eduLevelId==null||eduLevelId.equals("0")){   
    eduLevelId="null";
   }
   ArrayListUtilObject> eduLevelList=uess.getEduLevelIdByGradeNameInSpecialDetail(gradeName);
   ArrayListUtilObject> specialIdList=uess.getSpecialIdByGradeNameAndEduLevelIdInSpecialDetail(gradeName, eduLevelId);  
   mav.addObject("educationLevel", eduLevelList);
   mav.addObject("specialList", specialIdList);
   mav.setViewName("scoreManage/uniExamScore/eduLevelAndSpecialAjax");
   return mav;
  }
  @RequestMapping(value = "/specialByGradeNameAndEduLevelIdInSpecialDetail", method = RequestMethod.POST)
  public ModelAndView getSpecialByGradeNameAndEduLevelIdInSpecialDetail(HttpServletRequest request,
    HttpServletResponse response) throws JsonParseException, JsonMappingException, JSONException, IOException{
   String gradeName=request.getParameter("grade"); 
   String eduLevelId=request.getParameter("eduLevelId");
   System.out.println("grade:"+gradeName+"  eduLevelId:"+eduLevelId);
   if(gradeName==null||gradeName.equals("0")){   
    gradeName="null";
   }
   if(eduLevelId==null||eduLevelId.equals("0")){   
    eduLevelId="null";
   }
   ArrayListUtilObject> specialList=uess.getSpecialIdByGradeNameAndEduLevelIdInSpecialDetail(gradeName, eduLevelId);  
   mav.addObject("specialList", specialList);
   mav.setViewName("scoreManage/uniExamScore/specialAjax");
   return mav;
  }

后臺代碼沒有給出來,但應該看得懂,就是獲取后臺數據傳到eduLevelAndSpecialAjax.jsp和specialAjax.jsp頁面。這兩個頁面用于填充原頁面,通過ID來填充相應區域,兩個頁面代碼如下。
eduLevelAndSpecialAjax.jsp輔助頁面:
復制代碼 代碼如下:

td id="refreshEduLevelAndSpecialAjax">    //ID用于填充原頁面
    table>
    tr>
     td width="400px" align="left">層nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;次:select
      id="eduLevelId" name="eduLevelId" onchange="refreshSpecialAjax();">
       option value="0">--請選擇--/option>
       c:forEach items="${educationLevel}" var="educationLevel">
        option value="${educationLevel.id}">${educationLevel.name}/option>
       /c:forEach>
     /select>/td>
     td width="400px" align="left" id="refreshSpecialAjax">專nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;業:SELECT                               //ID用于填充原頁面
      NAME="specialId" id="specialId">
       option value="0">--請選擇--/option>
       c:forEach items="${specialList}" var="special">
        OPTION VALUE="${special.id}">${special.name}
       /c:forEach>
     /SELECT>/td>
     /tr>
    /table>
   /td>

specialAjax.jsp輔助頁面:
復制代碼 代碼如下:

td width="400" align="left" id="refreshSpecialAjax">專nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;業:SELECT
    NAME="specialId" id="specialId">    //ID用于填充原頁面
     option value="0">--請選擇--/option>
     c:forEach items="${specialList}" var="special">
      OPTION VALUE="${special.id}">${special.name}
     /c:forEach>
   /SELECT>/td>

這樣就在JSP頁面實現了填充。

您可能感興趣的文章:
  • JSP自定義標簽Taglib實現過程重點總結
  • jsp struts1 標簽實例詳解
  • jsp頁面中如何將時間戳字符串格式化為時間標簽
  • JSP自定義標簽rtexprvalue屬性用法實例分析
  • jsp自定義標簽用法實例詳解
  • JSP自定義分頁標簽TAG全過程
  • JSP中常用的JSTL fmt(format格式化)標簽用法整理
  • Jsp自定義標簽和方法詳解

標簽:云浮 江西 西藏 貴州 紅河 丹東 宿州 青島

巨人網絡通訊聲明:本文標題《JSP頁面中如何用select標簽實現級聯》,本文關鍵詞  JSP,頁面,中如,何用,select,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《JSP頁面中如何用select標簽實現級聯》相關的同類信息!
  • 本頁收集關于JSP頁面中如何用select標簽實現級聯的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 达尔| 北川| 页游| 灯塔市| 宜君县| 宜春市| 蓬溪县| 许昌市| 喀喇沁旗| 华宁县| 平罗县| 涞源县| 苏尼特右旗| 新宾| 会泽县| 红原县| 克山县| 博兴县| 鹤岗市| 尼玛县| 临猗县| 文安县| 芜湖市| 永登县| 阿克陶县| 嵩明县| 团风县| 陇西县| 同心县| 涪陵区| 象州县| 桓仁| 分宜县| 万山特区| 汕尾市| 勃利县| 乌什县| 久治县| 南昌县| 忻城县| 彰化市|