<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 19:56:43
        文檔

        Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表

        Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表:這次給大家?guī)鞟jax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表,Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。實現(xiàn)Ajax實現(xiàn)省市區(qū)三級級聯(lián),需要Java解析json技術(shù) 整體Demo下載地址如下: 點我下載address.html<
        推薦度:
        導(dǎo)讀Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表:這次給大家?guī)鞟jax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表,Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。實現(xiàn)Ajax實現(xiàn)省市區(qū)三級級聯(lián),需要Java解析json技術(shù) 整體Demo下載地址如下: 點我下載address.html<

        這次給大家?guī)鞟jax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表,Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。

        實現(xiàn)Ajax實現(xiàn)省市區(qū)三級級聯(lián),需要Java解析json技術(shù)
        整體Demo下載地址如下: 點我下載

        address.html

        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
        </head>
         <script type="text/javascript">
         /** 
         * 得到XMLHttpRequest對象 
         */
         function getajaxHttp() {
         var xmlHttp;
         try {
         // Firefox, Opera 8.0+, Safari 
         xmlHttp = new XMLHttpRequest();
         } catch (e) {
         // Internet Explorer 
         try {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
         try {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {
         alert("您的瀏覽器不支持AJAX!");
         return false;
         }
         }
         }
         return xmlHttp;
         }
         /** 
         * 發(fā)送ajax請求 
         * url--請求到服務(wù)器的URL 
         * methodtype(post/get) 
         * con (true(異步)|false(同步)) 
         * functionName(回調(diào)方法名,不需要引號,這里只有成功的時候才調(diào)用) 
         * (注意:這方法有二個參數(shù),一個就是xmlhttp,一個就是要處理的對象) 
         */
         function ajaxrequest(url, methodtype, con, functionName) {
         //獲取XMLHTTPRequest對象
         var xmlhttp = getajaxHttp();
         //設(shè)置回調(diào)函數(shù)(響應(yīng)的時候調(diào)用的函數(shù))
         xmlhttp.onreadystatechange = function() {
         //這個函數(shù)中的代碼在什么時候被XMLHTTPRequest對象調(diào)用?
         //當(dāng)服務(wù)器響應(yīng)時,XMLHTTPRequest對象會自動調(diào)用該回調(diào)方法
         if (xmlhttp.readyState == 4) {
         if (xmlhttp.status == 200) {
         functionName(xmlhttp.responseText);
         }
         }
         };
         //創(chuàng)建請求
         xmlhttp.open(methodtype, url, con);
         //發(fā)送請求
         xmlhttp.send();
         }
         window.onload=function(){
         ajaxrequest("addressSerlvet?method=provincial","POST",true,addrResponse);
         }
         //動態(tài)獲取省的信息
         function addrResponse(responseContents){
         var jsonObj = new Function("return" + responseContents)();
         for(var i = 0; i < jsonObj.addrList.length;i++){
         document.getElementById('select').innerHTML += 
         "<option value='"+jsonObj.addrList[i].id+"'>"
         +jsonObj.addrList[i].address+
         "</option>"
         }
         }
         //選中省后
         function pChange(){
         //先將市的之前的信息清除
         document.getElementById('selectCity').innerHTML="<option value='-1'>請選擇市</option>";
         //再將區(qū)的信息清除
         document.getElementById('selectArea').innerHTML="<option value='-1'>請選擇區(qū)</option>";
         //再將用戶的輸入清楚
         document.getElementById("addr").innerHTML="";
         var val = document.getElementById('select').value;
         if(val == -1){
         document.getElementById('selectCity')[0].selected = true;
         return;
         }
         //開始執(zhí)行獲取市
         ajaxrequest("addressSerlvet?method=city&provincial="+val,"POST",true,cityResponse);
         }
         //獲取市的動態(tài)數(shù)據(jù)
         function cityResponse(responseContents){
         var jsonObj = new Function("return" + responseContents)();
         for(var i = 0; i < jsonObj.cityList.length;i++){
         document.getElementById('selectCity').innerHTML += 
         "<option value='"+jsonObj.cityList[i].id+"'>"
         +jsonObj.cityList[i].address+
         "</option>"
         }
         }
         //選中市以后
         function cChange(){
         var val = document.getElementById('selectCity').value;
         //開始執(zhí)行獲取區(qū)
         ajaxrequest("addressSerlvet?method=area&cityId="+val,"POST",true,areaResponse);
         }
         //獲取區(qū)的動態(tài)數(shù)據(jù)
         function areaResponse(responseContents){
         var jsonObj = new Function("return" + responseContents)();
         for(var i = 0; i < jsonObj.areaList.length;i++){
         document.getElementById('selectArea').innerHTML += 
         "<option value='"+jsonObj.areaList[i].id+"'>"
         +jsonObj.areaList[i].address+
         "</option>"
         }
         }
         //點擊提交按鈕
         function confirM(){
         //獲取省的文本值
         var p = document.getElementById("select");
         var pTex = p.options[p.options.selectedIndex].text;
         if(p.value=-1){
         alert("請選擇省");
         return;
         }
         //獲取市的文本值
         var city = document.getElementById("selectCity");
         var cityTex = city.options[city.options.selectedIndex].text;
         if(city.value=-1){
         alert("請選擇市");
         return;
         }
         //獲取區(qū)的文本值
         var area = document.getElementById("selectArea");
         var areaTex = area.options[area.options.selectedIndex].text;
         if(area.value=-1){
         alert("請選擇區(qū)");
         return;
         }
         //獲取具體位置id文本值
         var addr = document.getElementById("addr").value;
         //打印
         document.getElementById("show").innerHTML = "您選擇的地址為 " + pTex + " " + cityTex + " " + areaTex + " " + addr;
         }
         </script>
        <body>
         <select id="select" onchange="pChange()">
         <option value="-1">請選擇省</option>
         </select>
         <select id="selectCity" onchange="cChange()">
         <option value='-1'>請選擇市</option>
         </select>
         <select id="selectArea" onchange="aChange()">
         <option value='-1'>請選擇市</option>
         </select>
         <input type="text" id="addr" />
         <button onclick="confirM();">確定</button>
         <p id="show"></p>
        </body>
        </html>

        AddressServlet.java

        package cn.bestchance.servlet;
        import java.io.IOException;
        import java.util.ArrayList;
        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import cn.bestchance.dao.AddressDao;
        import cn.bestchance.dao.impl.AddressDaoImpl;
        import cn.bestchance.entity.Address;
        import net.sf.json.JSONArray;
        import net.sf.json.JSONObject;
        @WebServlet("/addressSerlvet")
        public class AddressSerlvet extends HttpServlet {
         private static final long serialVersionUID = 1L;
         private AddressDao dao = new AddressDaoImpl();
         protected void doGet(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
         doPost(request, response);
         }
         /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         * response)
         */
         protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
         response.setCharacterEncoding("utf-8");
         response.setContentType("text/html;charset=utf-8");
         String method=request.getParameter("method");
         if("provincial".equals(method)){
         getProvincial(request, response);
         }
         if("city".equals(method)){
         getCity(request, response);
         }
         if("area".equals(method)){
         getArea(request, response);
         }
         }
         /**
         * 根據(jù)市id獲取該市下的區(qū)的全部信息
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
         protected void getArea(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
         String cityId = request.getParameter("cityId");
         // 從數(shù)據(jù)庫中查詢省的信息
         ArrayList<Address> areaList = dao.getAreaByCityId(Integer.parseInt(cityId));
         // 將集合轉(zhuǎn)成json字符串
         JSONObject jsonObj = new JSONObject();
         JSONArray jsonArray = JSONArray.fromObject(areaList);
         jsonObj.put("areaList", jsonArray);
         String jsonDataStr = jsonObj.toString();
         response.getWriter().print(jsonDataStr);
         }
         /**
         * 獲取省的信息 并相應(yīng)
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
         protected void getProvincial(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
         // 從數(shù)據(jù)庫中查詢省的信息
         ArrayList<Address> addrList = dao.getProvince();
         // 將集合轉(zhuǎn)成json字符串
         JSONObject jsonObj = new JSONObject();
         JSONArray jsonArray = JSONArray.fromObject(addrList);
         jsonObj.put("addrList", jsonArray);
         String jsonDataStr = jsonObj.toString();
         response.getWriter().print(jsonDataStr);
         }
         /**
         * 獲取市的信息并相應(yīng)
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
         protected void getCity(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
         String provinceId = request.getParameter("provincial");
         // 從數(shù)據(jù)庫中查詢省的信息
         ArrayList<Address> addrList = dao.getCityByProvinceId(Integer.parseInt(provinceId));
         // 將集合轉(zhuǎn)成json字符串
         JSONObject jsonObj = new JSONObject();
         JSONArray jsonArray = JSONArray.fromObject(addrList);
         jsonObj.put("cityList", jsonArray);
         String jsonDataStr = jsonObj.toString();
         response.getWriter().print(jsonDataStr);
         }
        }

        AddressDao.java

        package cn.bestchance.dao;
        import java.util.ArrayList;
        import cn.bestchance.entity.Address;
        public interface AddressDao {
         /**
         * 獲取省的id和名稱
         * @return
         */
         ArrayList<Address> getProvince();
         /**
         * 根據(jù)省的id獲取市的信息
         * @param provinceId
         * @return
         */
         ArrayList<Address> getCityByProvinceId(int provinceId);
         /**
         * 根據(jù)市的id獲取區(qū)的信息
         * @param cityId
         * @return
         */
         ArrayList<Address> getAreaByCityId(int cityId);
        }

        AddressDaoImpl.java

        package cn.bestchance.dao.impl;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.util.ArrayList;
        import cn.bestchance.dao.AddressDao;
        import cn.bestchance.entity.Address;
        import cn.bestchance.util.DBUtil;
        public class AddressDaoImpl implements AddressDao {
         private DBUtil db = new DBUtil();
         @Override
         public ArrayList<Address> getProvince() {
         ArrayList<Address> addrList = new ArrayList<Address>();
         db.openConnection();
         String sql = "select * from province";
         ResultSet rs = db.excuteQuery(sql);
         try {
         while(rs.next()){
         Address addr = new Address();
         addr.setId(rs.getInt(2));
         addr.setAddress(rs.getString(3));
         addrList.add(addr);
         }
         } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }finally{
         if(rs != null){
         try {
         rs.close();
         } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
         }
         db.closeResoure();
         }
         return addrList;
         }
         @Override
         public ArrayList<Address> getCityByProvinceId(int provinceId) {
         ArrayList<Address> addrList = new ArrayList<Address>();
         db.openConnection();
         String sql = "select * from city where fatherID = " + provinceId; //431200
         ResultSet rs = db.excuteQuery(sql);
         try {
         while(rs.next()){
         Address addr = new Address();
         addr.setId(rs.getInt(2));
         addr.setAddress(rs.getString(3));
         addrList.add(addr);
         }
         } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }finally{
         if(rs != null){
         try {
         rs.close();
         } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
         }
         db.closeResoure();
         }
         return addrList;
         }
         @Override
         public ArrayList<Address> getAreaByCityId(int cityId) {
         ArrayList<Address> addrList = new ArrayList<Address>();
         db.openConnection();
         String sql = "select * from area where fatherID = " + cityId; //431200
         ResultSet rs = db.excuteQuery(sql);
         try {
         while(rs.next()){
         Address addr = new Address();
         addr.setId(rs.getInt(2));
         addr.setAddress(rs.getString(3));
         addrList.add(addr);
         }
         } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }finally{
         if(rs != null){
         try {
         rs.close();
         } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
         }
         db.closeResoure();
         }
         return addrList;
         }
        }

        實體類Address.java

        package cn.bestchance.entity;
        public class Address {
         @Override
         public String toString() {
         return "Address [id=" + id + ", address=" + address + "]";
         }
         private int id;
         private String address;
         public int getId() {
         return id;
         }
         public void setId(int id) {
         this.id = id;
         }
         public String getAddress() {
         return address;
         }
         public void setAddress(String address) {
         this.address = address;
         }
         public Address() {
         super();
         // TODO Auto-generated constructor stub
         }
         public Address(int id, String address) {
         super();
         this.id = id;
         this.address = address;
         }
        }

        相信看了本文案例你已經(jīng)掌握了方法,更多精彩請關(guān)注Gxl網(wǎng)其它相關(guān)文章!

        推薦閱讀:

        ajax怎樣提交form表單與實現(xiàn)文件上傳

        Ajax向后臺傳輸json格式數(shù)據(jù)出現(xiàn)錯誤應(yīng)如何處理

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表

        Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表:這次給大家?guī)鞟jax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表,Ajax+mysq實現(xiàn)省市區(qū)三級聯(lián)動列表的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。實現(xiàn)Ajax實現(xiàn)省市區(qū)三級級聯(lián),需要Java解析json技術(shù) 整體Demo下載地址如下: 點我下載address.html<
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产免费私拍一区二区三区| 最近2019免费中文字幕6| 成年在线网站免费观看无广告| 亚洲成a人片在线观看日本| 美女被免费网站在线视频免费| 成人免费观看一区二区| 亚洲乱亚洲乱淫久久| 精品国产污污免费网站| 亚洲精品少妇30p| 99久久国产精品免费一区二区 | 久久国产色AV免费看| 国产亚洲精品无码成人| 97国免费在线视频| 亚洲乱码中文字幕综合 | 色综合久久精品亚洲国产| 永久黄网站色视频免费观看| 人人狠狠综合久久亚洲| 免费a级毛片大学生免费观看 | 91网站免费观看| 亚洲国产精品成人精品小说| 国产免费看JIZZ视频| 亚洲av色香蕉一区二区三区蜜桃| 亚洲AV无码专区日韩| 巨胸狂喷奶水视频www网站免费| 亚洲国产精品无码专区在线观看 | 国产成人免费高清激情明星| 亚洲国产成人九九综合| 免费观看的av毛片的网站| 春意影院午夜爽爽爽免费| 亚洲啪啪综合AV一区| 免费福利视频导航| 精品亚洲福利一区二区| 亚洲香蕉网久久综合影视| 在免费jizzjizz在线播 | 美女扒开屁股让男人桶爽免费| 亚洲男人第一无码aⅴ网站| 免费精品无码AV片在线观看| 亚洲欧美国产国产一区二区三区| 亚洲麻豆精品国偷自产在线91| 四虎成人精品永久免费AV| 亚洲heyzo专区无码综合|