select二级联动

来源:互联网 发布:软件开发行业分析 编辑:程序博客网 时间:2024/04/29 13:04

jquery ajax

<!-- 归属地联动 --><script type='text/javascript' src='<%=request.getContextPath() %>/js/jquery.js'></script><script type='text/javascript'>$(document).ready(function() {  $("#ownerAddress").change(  function(){  $.ajax({type : "POST",dataType  : "json",    url :"<%=request.getContextPath()%>/queryOwnerAddress.jspx?ownerAddress="+document.getElementById('ownerAddress').value,success : function(result){$("#ownerAddressNode option").remove();$("#ownerAddressNode").append("<option>请选择</option>");if( result != null && result != "" ){for(var i=0;i<result.length;i++){$("#ownerAddressNode").append("<option value='"+result[i].id+"'>"+result[i].name+"</option>");}}}});});});</script>


 java代码

/** * 查询归属地信息 * @param request * @param response * @return */@RequestMapping(value = "/queryOwnerAddress.jspx", method = RequestMethod.POST)public String queryOwnerAddress(HttpServletRequest request, HttpServletResponse response) {String ownerAddress =  request.getParameter("ownerAddress");JSONArray jsonArray = clubService.queryOwnerAddress(ownerAddress);StringUtil.webCommonResponse(request, response, jsonArray);return null;}


public JSONArray queryOwnerAddress(String ownerAddress) {JSONArray jsonArray = new JSONArray();if("1".equals(ownerAddress)) {JSONObject json1 = new JSONObject();json1.put("id", 1);json1.put("name", "苏州市区");jsonArray.add(json1);JSONObject json2 = new JSONObject();json2.put("id", 2);json2.put("name", "苏州郊区");jsonArray.add(json2);}if("2".equals(ownerAddress)) {JSONObject json1 = new JSONObject();json1.put("id", 1);json1.put("name", "南京市区");jsonArray.add(json1);JSONObject json2 = new JSONObject();json2.put("id", 2);json2.put("name", "南京郊区");jsonArray.add(json2);JSONObject json3 = new JSONObject();json3.put("id", 3);json3.put("name", "其它地区");jsonArray.add(json3);}return jsonArray;}


public static void webCommonResponse(HttpServletRequest req, HttpServletResponse rsp, Object json) {rsp.setContentType("text/html; charset=UTF-8");rsp.setCharacterEncoding("UTF-8");rsp.setHeader("Cache-Control", "no-cache");String encoding = req.getHeader("Accept-Encoding");if (encoding != null && encoding.indexOf("gzip") != -1) {rsp.setHeader("Content-Encoding", "gzip");GZIPOutputStream out = null;try {out = new GZIPOutputStream(rsp.getOutputStream());out.write(json.toString().getBytes("UTF-8"));}catch (IOException e) {// LOGGER.error("", e);}finally {try {out.flush();out.finish();out.close();}catch (IOException e) {// LOGGER.error("", e);}}return;}try {rsp.getWriter().println(json.toString());rsp.getWriter().flush();}catch (IOException e) {// LOGGER.error("", e);}}