jquery json实现二级动态联动
以下为代码!需要导入json架包
function getCity1(){ var unitid = document.getElementById('addformunitid').value; $.getJSON("../unit/getcity.do?unitid="+unitid, "", function(response){ //$ ("#departmentid").show();//将隐藏的select显示出来 $ ("#adddepartmentid option").remove();//将select中的信息清空 $ ("#adddepartmentid").attr("disabled",false); //alert(response); $.each(response,function(i){ //encodeURI(response[i]); //encodeURI(fileName) //将后台传回的信息打印到页面中 $ ("#adddepartmentid").append ("<option value="+response[i].id+">"+ response[i].departmentname +"</option>"); }); }); }
<tr> <td class="td_title">所属单位<em>*</em></td> <td> <select name="eform.unitid" id="addformunitid" onChange="getCity1();"> <option value="0">请选择</option> <#list unitList as ul> <option value="${ul.id}">${ul.unitname}</option> </#list> </select> </td> <td class="td_title">部门<em>*</em></td> <td> <select name="eform.departmentid" id="adddepartmentid" disabled="disabled"> <option value=" ">请选择</option> </select> </td> <td class="td_title">职位<em>*</em></td> <td> <select name="eform.positionid"> <option value=" ">请选择-</option> <#list positionList as pl> <option value="${pl.id}">${pl.positionname}</option> </#list> </select> </td> </tr>
<!-- 单位,部门二级联动 -->
<package name="unit" extends="webwork-default" namespace="/unit"> <action name="getcity" class="com.hnbus.action.unit.EmployeeJsonAction" method="getcity" > <interceptor-ref name="defaultStack"/> </action> </package>
/* *文件名:EmployeeJsonAction.java *描 述:二级联动action *版 权:********有限公司 Copyright (c) 2008-2013,All Rights Reserved. *创建者:向洁 *修改者:向洁 */ package com.hnbus.action.unit; import java.io.PrintWriter; import java.net.URLDecoder; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import org.apache.log4j.Logger; import org.eps.action.BaseAction; import com.hnbus.model.Department; import com.hnbus.model.Employee; import com.hnbus.service.unit.EmployeeService; import com.opensymphony.webwork.ServletActionContext; /** * @Description (二级联动action) * @author 向洁 * @version JOHOM V1.0 * @Date 2013-8-4-下午10:54:05 * @see */ public class EmployeeJsonAction extends BaseAction{ private static final Logger logger = Logger.getLogger(EmployeeAction.class); @Resource private EmployeeService employeeService; private EmployeeForm1 eform; private String unitid; public String getcity() throws Exception { logger.info("选择单位ID"); //获取前台传来的值 String unitid = this.getRequest().getParameter("unitid"); HttpServletResponse response = ServletActionContext.getResponse(); /*设置字符集为'UTF-8'*/ response.setContentType("text/plain;charset=utf-8"); List<Department> department = employeeService.findSameDepartmentlist(unitid); ServletActionContext.getRequest().getSession().setAttribute("department",department); //将list转换成JSON对象 JSONArray jr = JSONArray.fromObject(department); PrintWriter out = this.getResponse().getWriter(); //将JSON对象转换成String类型传入前台 String jsonUser = jr.toString(); out.print(jsonUser); out.flush(); out.close(); return null; } /** * 变量:employeeService * @return 变量:employeeService */ public EmployeeService getEmployeeService() { return employeeService; } /** * 变量:eform * @return 变量:eform */ public EmployeeForm1 getEform() { return eform; } /** * 变量:eform * @param eform (Set the variable:eform) */ public void setEform(EmployeeForm1 eform) { this.eform = eform; } /** * 变量:employeeService * @param employeeService (Set the variable:employeeService) */ public void setEmployeeService(EmployeeService employeeService) { this.employeeService = employeeService; } /** * 变量:logger * @return 变量:logger */ public static Logger getLogger() { return logger; } /** * 变量:unitid * @return 变量:unitid */ public String getUnitid() { return unitid; } /** * 变量:unitid * @param unitid (Set the variable:unitid) */ public void setUnitid(String unitid) { this.unitid = unitid; } } class EmployeeForm1 extends Employee{}