日期:2014-05-16  浏览次数:20665 次

三级联动通过AJAX 将后台的数据返回在页面上。 主要是下拉框的联动。
//根据浏览器的类型创建xmlHttpRequest对象
function createXmlHttpRequest(){
  if(window.ActiveXObject){
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequest()){
    return new XMLHttpRequest();
  }
}
var xmlHttpRequest;
//异步响应函数
function search(para1,para2){
  var url="/projectNameQuery.jsp?depid="+para1+"&projectType="+para2;
  xmlHttpRequest=createXmlHttpRequest();
  xmlHttpRequest.onreadystatechange=callback;
  xmlHttpRequest.open("GET",url,true);
  xmlHttpRequest.send(null);

}
//回调函数
function callback(){
  var projectName=document.getElementById("projectname");
  //请求被成功响应,已经接收到结果
  if(xmlHttpRequest.readyState==4&&xmlHttpRequest.status==200){
    var myJson = eval("(" + xmlHttpRequest.responseText + ")");
    projectName.length=0;
    projectName.options.add(new Option("请选择事项名称", "-1"));
    for (i = 0; i < myJson.length; i ++) {
      projectName.options.add(new Option(myJson[i].name, myJson[i].keyid));
    }
  } else {
    projectName.length = 0;
    projectName.options.add(new Option("请等待...","-1"));
  }
}
function projectLoad(){
  var url="/projectNameQuery.jsp?depid=-1&projectType=-1";
  xmlHttpRequest=createXmlHttpRequest();
  xmlHttpRequest.onreadystatechange=callback;
  xmlHttpRequest.open("GET",url,true);
  xmlHttpRequest.send(null);
}
//下拉框改变事件
function changeSelect(){
  var zxdep=document.getElementById("zxdep");
  var projectType=document.getElementById("projecttype");
  var projectName=document.getElementById("projectname");
  projectName.length=0;
  if(zxdep.value==-1&&projectType.value==-1){
    projectName.options.add(new Option("请选择事项名称","-1"));
  }
  else{
    search(zxdep.value,projectType.value);
  }
}

<%@ page language="java" pageEncoding="GBK"%>
<%@ include file="/WEB-INF/include/getOatDBBean.jsp"%>
<%
  String depId = request.getParameter("depid");
  //String depId="2c90808e2aefd7cf012af40289830069";
  String projectType = request.getParameter("projectType");
  String whereSQL = "where 1=1";
  String[][] result = null;
  if(depId != null && projectType != null && !"-1".equals(depId) && !"-1".equals(projectType)){
    whereSQL += " and t.xmlx='"+projectType+"'and t.department_id='"+depId+"' ";
  }
  if(depId!=null&&projectType!=null&&!"-1".equals(depId)){
    whereSQL += " and t.department_id='"+depId+"' ";
  }
  if(depId!=null&&projectType!=null&&!"-1".equals(projectType)){
    whereSQL += " and t.xmlx='"+projectType+"'";
  }
  String sql = "select t.keyid,t.name from projectinfo t "+whereSQL;
  String callback = "[";
  try{

    result = otaDbbean.getManyRows_ManyColumns(sql);
    for(int i=0; i<result.length; i++){
      callback += "{keyid:'" + result[i][0] + "',name:'" + result[i][1] + "'},";
    }
    callback = callback.substring(0,callback.length()-1) + "]";
    response.getWriter().write(callback);
  }catch (Exception ex){
     System.out.println("projectNameQuery.jsp error : " + ex);
     callback += "]";
     response.getWriter().write(callback);
  } finally {
     otaDbbean.cleanUp();
  }
%>