<!-- 查询条件列表开始 --> <form id="prettyTypeForm" name="prettyTypeForm" method="post"> <div id="retrieveCondition"> <table style="width: 950px;" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="25%" height="27" align="right" class="textAnmedia" bgcolor="#E7F0F7">网络类型:</td> <td width="25%" height="27" align="left" class="textAnmedia" bgcolor="#E7F0F7"> <select id="wllx" name="wllx" style="width: 135px;"> <option value="-1">请选择...</option> <option value="0">2G</option> <option value="1">3G</option> </select> </td> <td width="25%" height="27" align="right" class="textAnmedia" bgcolor="#E7F0F7">类型名称:</td> <td width="25%" height="27" align="left" class="textAnmedia" bgcolor="#E7F0F7"> <input type="text" id="name" name="name" /> </td> </tr> <tr> <td width="25%" height="27" align="right" class="textAnmedia" bgcolor="#E7F0F7">请选择省份:</td> <td width="25%" height="27" align="left" class="textAnmedia" bgcolor="#E7F0F7"> <select id="province" name="province" style="width: 135px;"> <option value="-1" >请选择...</option> <beans:tagHandle id="tagProvince"> <database:prepareQuery id="<%=tagProvince%>" scope="page"> select id,city_code,city_name from tb_area_city where leve = 1 </database:prepareQuery> <database:rows id="db2" query='<%=tagProvince%>'> <option value="<%=db2.get("id") %>" ><%=db2.get("city_name") %></option> </database:rows> <database:release query='<%=tagProvince%>'/> </beans:tagHandle> </select> </td> <td width="25%" height="27" align="right" class="textAnmedia" bgcolor="#E7F0F7">请选择城市:</td> <td width="25%" height="27" align="left" class="textAnmedia" bgcolor="#E7F0F7"> <select id="city" name="city" style="width: 135px;"><option value="-1" >请选择...</option></select> <span id="cityTip" style="color: red" mce_style="color: red"></span> </td> </tr> <tr> <td width="98%" colspan="4" height="27" align="right" class="textAnmedia" bgcolor="#E7F0F7"> <input id="formSubmit" name="formSubmit" type="button" value=" 查询 " /> </td> </tr> </table> </div> </form> <!-- 查询条件列表结束 --> 1.这是JSP文件 //靓号类型后台查询JS用于prettyTypeRetrieve.jsp $(document).ready(function() { $("#province").change(function(){ //清空城市 clearSel(document.getElementById("city")); //请求结果 $.post('areaLinkage.do',{ //参数一 province_id: $("#province").val() }, //回调函数 function(result) { $(result).find("entry").each(function(i){ var optionText = $(this).find("optionText").text(); var optionValue = $(this).find("optionValue").text(); var option = new Option(optionText,optionValue); $("#city").append("<option value="+optionText+">"+optionValue+"</option>"); }); }, //返回类型 "xml" ); // 清空下拉列表 function clearSel(oSelect){ while(oSelect.childNodes.length>0){ oSelect.removeChild(oSelect.childNodes[0]); } } }); }); 2.这是JS代码 public class AreaLinkageAction extends Action { /** * 实现二级联动 */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/xml; charset=utf-8"); response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); AreaLinkageDao dao = new AreaLinkageDao(); List list = new ArrayList(); StringBuilder strXML = new StringBuilder(); try { list = dao.areaLinkage(request.getParameter("province_id"), this.getServlet() .getServletContext()); strXML.append("<?xml version=/"1.0/" encoding=/"UTF-8/"?>"); strXML.append("<selectChoice>"); if (list.size() > 0) { strXML.append("<entry>"); strXML.append("<optionText>"); strXML.append("-1"); strXML.append("</optionText>"); strXML.append("<optionValue>"); strXML.append("请选择..."); strXML.append("</optionValue>"); strXML.append("</entry>"); for (Iterator it = list.iterator(); it.hasNext();) { Hashtable ht = new Hashtable(); ht = (Hashtable) it.next(); strXML.append("<entry>"); strXML.append("<optionText>"); strXML.append("" + ht.get("id").toString() + ""); strXML.append("</optionText>"); strXML.append("<optionValue>"); strXML.append("" + ht.get("city_name").toString() + ""); strXML.append("</optionValue>"); strXML.append("</entry>"); } strXML.append("</selectChoice>"); } String xml = strXML.toString(); out.write(xml); out.flush(); } catch (Exception e) { e.printStackTrace(); } out.close(); return null; } } 3.这是Action代码 public class AreaLinkageDao { /** * 根据省ID查询城市信息 * * @param parameter * @param servletContext * @return */ public List areaLinkage(String provinceId, ServletContext servletContext) throws Exception { RootDaoInterface rootDao = (RootDaoInterface) DataSourceHelper .getDaoInterfae(servletContext); String sql = "select id,city_code,city_name from tb_area_city where pid = ?"; return rootDao.preparedQuery(sql, new String[] { provinceId }); } } 4.这是DAO代码 所有代码全部完成记得要在调用的页面上引用JS