前端之家收集整理的这篇文章主要介绍了
ajax级联菜单,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.
前台页面 <script type="text/javascript" src="<%=basePath %>script/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#district").change(function(){ var p_id=$(this).val(); var strHTML=""; if(p_id==0){ strHTML="<option value=\"0\">-请选择市--</option>" $("#street").html(strHTML); return; } $.ajax({ type:"post",url:"ajaxServer!ajaxServer",data:{pid:p_id},dataType:"json",success: function(data){ var len=data.length; for(var i=0;i<len;i++){ strHTML+="<option value=\""+data[i].id+"\">"+data[i].name+"</option>"; } $("#street").html(strHTML); },error:function(){ alert("解析数据
错误"); } }); }); }); </script> 2.
后台action public String ajaxServer(){ Integer p_id=pid==null?null:Integer.parseInt(pid.toString()); District district=new District(); district.setId(p_id); Set<Street> listStreet=districtService.queryStreet(district); //ajax
解决对象里面包含对象 JsonConfig config=new JsonConfig(); HttpServletResponse response=ServletActionContext.getResponse(); config.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object source,String name,Object value) { if(name.equals("district")||name.equals("house")){ return true; }else { return false; } } }); JSONArray jsonArray=JSONArray.fromObject(listStreet,config); String jsonStr=jsonArray.toString(); System.out.println(jsonStr); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try { response.getWriter().write(jsonStr); response.getWriter().close();//
关闭输出流 } catch (IOException e) { e.printStackTrace(); } return SUCCESS; } 3.struts
配置文件跟普
通配置一样 <action name="ajaxServer" class="houseAction" method="ajaxServer"> </action>