Ajax二级联动下拉框

来源:互联网 发布:苹果6蜂窝数据快捷键 编辑:程序博客网 时间:2024/05/18 01:36

JSP:

 

课程名称:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/options.js"></script>

 <html:select property="icourseId" onchange="change(this)" style="width:180px;">
      <html:option value="0">----请选择以下课程----</html:option>
      <html:options collection="clist" labelProperty="ccourseName"                    property="icourseId"></html:options>
</html:select>
课程章节:
 <select name="ichapterId" id="result" style="width:180px;">
 <option value="">----请选择以下章节----</option>
</select>

 

******************************************************************************
options.js:

 

function change(option){
 var opvalue = option.value;
 //设置与服务端进行交互的URL(包括中文处理)
 var url = "options.do?opvalue="+encodeURI(encodeURI(opvalue));
 url = covertURL(url);
 //与服务端进行交互,并显示数据
 $.get(url,null,function(data){$("#result").html(data);});
}
//处理缓存问题
function covertURL(url){
   var timeURL = (new Date()).valueOf();
   if(url.indexOf("?") >= 0){
       url = url + "&t=" + timeURL;
   }else{
       url = url + "?t=" + timeURL;
   }
   return url;
  }

 

***************************************************************************
ACTION:

 

public class OptionsAction extends Action {
 private WebService webservice;
 
 public void setWebservice(WebService webservice) {
  this.webservice = webservice;
 }
 
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  response.setContentType("text/html;charset=UTF-8");
  String cid = request.getParameter("opvalue").trim();
  PrintWriter out = null;
  String responseContext = "";
  
  try {
   List<Chapter>  list = webservice.selectOptionsReport(cid);
   if(!list.isEmpty()){
       for(Chapter chapter : list){
            responseContext += "<option value='"+chapter.getIchapterId()+"'>"+chapter.getCchapterName()+"</option>";
       }
   }
   
  } catch (Exception e1) {
   
   e1.printStackTrace();
  }
  try {


   out = response.getWriter();


  } catch (IOException e) {
   
   e.printStackTrace();
  }
  
  out.print(responseContext);
  out.flush();
  out.close();
  return null;
 }
}

原创粉丝点击