AJAX省郊區三級聯動下拉菜單(java版)。本站提示廣大學習愛好者:(AJAX省郊區三級聯動下拉菜單(java版))文章只能為提供參考,不一定能成為您想要的結果。以下是AJAX省郊區三級聯動下拉菜單(java版)正文
此小法式的功效重要是采取異步要求方法從數據庫中調取省郊區信息顯示到下拉列表:
代碼以下:
樹立數據庫中的代碼和一些設置裝備擺設文件信息就省略了,重要有JavaScript中的代碼為:
$(document).ready(function(){ $.get("getProvince.do", function(result){ $("#showp").html(result); }); }) var xmlhttp; function mysend(str){ $(document).ready(function(){ $("#show2").html(""); }) var show = document.getElementByIdx_x_x_x_x_x_x_x_x_x("show"); show.innerHTML = ""; var province = document.getElementByIdx_x_x_x_x_x_x_x_x_x("province").value; if(province!=0){ if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200){ show.innerHTML = xmlhttp.responseText; } } var ss = encodeURIComponent(str); xmlhttp.open("GET","getCity.do?provinceid="+ss,true); xmlhttp.send(null); } } function myarea(str){ if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200){ var show2 = document.getElementByIdx_x_x_x_x_x_x_x_x_x("show2"); show2.innerHTML = xmlhttp.responseText; } } var ss = encodeURIComponent(str); xmlhttp.open("GET","getArea.do?cityid="+ss,true); xmlhttp.send(null); }
html頁面中的代碼為:
地點地
action中的代碼為:
package mobi.zhangsheng.jiejia.action; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.springframework.stereotype.Controller; import mobi.zhangsheng.jiejia.domain.Areas; import mobi.zhangsheng.jiejia.service.AgentsService; import mobi.zhangsheng.jiejia.service.AreasService; @Controller public class ProvinceAction { private int provinceid; private int cityid; @Resource private AreasService as; @Resource private AgentsService ags; public int getProvinceid() { return provinceid; } public void setProvinceid(int provinceid) { this.provinceid = provinceid; } public int getCityid() { return cityid; } public void setCityid(int cityid) { this.cityid = cityid; } public void getProvince(){ List provinceList = as.getAreasPrvinceList(); HttpServletResponse resp= ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); //resp.setContentType("xml"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); try { PrintWriter out = resp.getWriter(); out.print(""); //out.print("shanghai"); } catch (IOException e) { e.printStackTrace(); } } public void getCity(){ List cityList = as.getAreasCityList(provinceid); HttpServletResponse resp= ServletActionContext.getResponse(); //resp.setContentType("xml"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); try { PrintWriter out = resp.getWriter(); out.print(""); //out.print("shanghai"); } catch (IOException e) { e.printStackTrace(); } } public void getArea(){ List areaList = as.getAreasCityList(cityid); if(areaList.size()==0){ }else{ HttpServletResponse resp= ServletActionContext.getResponse(); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); try { PrintWriter out = resp.getWriter(); out.print(""); } catch (IOException e) { e.printStackTrace(); } } } }
重要的功效代碼都在下面了,若有不懂的請接洽QQ:1037139984,祝年夜家都有好的成長,配合進修,配合生長。