AJAX省市區三級聯動下拉式功能表(java版)_java

來源:互聯網
上載者:User

此小程式的功能主要是採用非同步請求方式從資料庫中調取省市區資訊顯示到下拉式清單:

代碼如下:

建立資料庫中的代碼和一些設定檔資訊就省略了,主要有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;@Controllerpublic class ProvinceAction {private int provinceid;private int cityid;@Resourceprivate AreasService as;@Resourceprivate 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,祝大家都有好的發展,共同學習,共同成長。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.