select二级联动

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);
		}
	}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值