用AJAX获取二级、三级下拉列表(例子)

 

-------------------------------js代码------------------------------------------

 

<script type="text/javascript">

function getChapter(oid){
    
     
  xmlURL = "theme.do?method=getChapater&&courseId="+oid.value;
   var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //这个对象用来发出Http请求
  xmlDoc.async="false"; //同步、异步
  xmlDoc.load(xmlURL); //发出请求 
  xmlObj=xmlDoc.documentElement; //根节点 
  document.form1.chapterId.options.length = 0; //清空章节下拉框  
  if(xmlObj.childNodes.length==0)
  {
     document.forms[0].chapterId.add(document.createElement("OPTION"));
     document.forms[0].chapterId.options[0].text="暂无"
  } 
  else
  {  
    for (var i=0; i<xmlObj.childNodes.length ; i++) {
   label=xmlObj.childNodes(i).text;
   values=xmlObj.childNodes[i].getAttribute("id");
   document.form1.chapterId.add(document.createElement("OPTION"));
   document.form1.chapterId.options[i].text = label;
   document.form1.chapterId.options[i].value= values;
   }
  }     
  }
  
  function getLore(oid){  
  xmlURL = "theme.do?method=getLore&&chapterId="+oid.value;
 
  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //这个对象用来发出Http请求
  xmlDoc.async="false"; //同步、异步
  xmlDoc.load(xmlURL); //发出请求 
  xmlObj=xmlDoc.documentElement; //根节点 
  document.form1.loreId.options.length = 0; //清空章节下拉框  
  
  if(xmlObj.childNodes.length==0)
  {
     document.forms[0].loreId.add(document.createElement("OPTION"));
     document.forms[0].loreId.options[0].text="暂无"
  } 
  else
  {  
  for (var i=0; i<xmlObj.childNodes.length ; i++) {
   label=xmlObj.childNodes(i).text;
   values=xmlObj.childNodes[i].getAttribute("id");
   document.form1.loreId.add(document.createElement("OPTION"));
   document.form1.loreId.options[i].text = label;
   document.form1.loreId.options[i].value= values;
   }
  }
  }
  </script>

 

 

 

------------------------------------ jsp页面------------------------------------------

<table width="100%" border="0" cellspacing="0"
                cellpadding="0">
                <tr>
                 <td width="44%" align="left">
                  <select name="courseId" οnchange=getChapter(this)>
                   <option value="0" selected="selected">
                    请选择课程
                   </option>
                   <c:forEach items="${courseList}" var="course">
                    <option value="${course.courseId}">
                     ${course.courseName}
                    </option>
                   </c:forEach>
                  </select>
                  <select name="chapterId" οnchange=getLore(this)>
                   <option value="0" selected="selected">
                    请选择章节
                   </option>
                  </select>
                  <select name="loreId">
                   <option value="0" selected="selected">
                    请选择知识点
                   </option>
                  </select>
                 </td>
                </tr>
               </table>

 

---------------------------------------------------JAVA代码 -----------------------------------

/**
  * 根据课程编号得到该编号下的所有章节
  *
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws IOException
  */
 public void getChapater(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {

  ChapterForm chapterForm = new ChapterForm();
  int courseId = RequestUtil.getIntParameter(request, "courseId", 0);
  if (courseId != 0) {
   chapterForm.setCourseId(courseId);
   Collection collection = baseFactory.getLoreDaoInter().getChapters(
     chapterForm);

   PrintWriter out = null;
   try {
    response.setContentType("text/xml; charset=GB2312");
    out = response.getWriter();
    out.println("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
    out.println("<chapters>");
    for (Iterator iter = collection.iterator(); iter.hasNext();) {
     ChapterForm chapterForm1 = (ChapterForm) iter.next();
     out.println("<chapter id=/"" + chapterForm1.getChapterId()
       + "/">" + chapterForm1.getChapterName()
       + "</chapter>");
    }
    out.println("</chapters>");
    out.flush();
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } else {
   PrintWriter out = null;
   try {
    response.setContentType("text/xml; charset=GB2312");
    out = response.getWriter();
    out.println("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
    out.println("<chapters>");
    out.println("<chapter id=/"" + 0 + "/">" + "--请选择--"
      + "</chapter>");
    out.println("</chapters>");
    out.flush();
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值