jquery异步更改select

前台页面:
function selectChange(id){
 jQuery.get("managercheck_getCarListByType.do?carType.id="+id,function(data){
 if(data!="nodata"){
 //var json=[{'id':'33','carCode':'ddd'},{'id':'33','carCode':'ddd'}];
 var json=eval(data);
 processData(json);
 }
 else processData(null);
 });
 }

function processData(json){
 var select=document.getElementById("carSelect");
 if(select.options.length > 1){
  var a = select.options.length;
   while(a > 1){
    select.removeChild(select.options[--a]);
     }
   }
   if(json!= null){
   for(var i = 0;i < json.length;i++){
    select.options.add(new Option(json[i].carCode,json[i].carId));
       }
     }
  }

 

后台:
 public String getCarListByType(){

  if(carType!=null&&carType.getId()!=null){
  carList=carService.findCarsByType(carType);
  System.out.println(carList.size());
  HttpServletResponse response = ServletActionContext.getResponse();
  
  try {
   PrintWriter out = response.getWriter();
   StringBuffer sb=new StringBuffer();
   sb.append("[");
   if(carList!=null&&carList.size() > 0){
    response.setContentType("text;charset=gbk");
    for(Car car : carList){
     sb.append("{'carId':'"+car.getId()+"',"+"'carCode':'"+car.getCarCode()+"'},");
    }
    sb.replace(sb.length()-1, sb.length(), "");
    sb.append("]");
    out.println(sb.toString());
   }
   else {
    response.setContentType("text;charset=gbk");
    out.print("nodata");
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }
  return null;
 }

你可能感兴趣的:(jquery,json)