<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ajax.js:
    var prjName="/projectName/";
    var ajaxObj;

    function createAjaxObject(){
        
    try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){};
        
    try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){};
        
    try{return new XMLHttpRequest();}catch(e){};
        alert(
    "XmlHttpRequest not supported!");
        
    return null;
    }

    function $(id){
         
    return document.getElementById(id);
    }
    city.jsp核心代碼:
    <fieldset><legend>添加城市</legend>
                                    
    <table>
                                        
    <tr>
                                        
    <td>
                                                
    <select name="countries" style="width:95px" onchange="changeCountry(this)">
                                                    
    <%    
                                                        
                                                        List
    <Country> counties = (List<Country>)request.getAttribute("countryLs");
                                                        
                                                        out.print(
    "<option value='0'>----請選擇----</option>");
                                                        
    if(counties!=null || counties.size()>0){
                                                            
    for (int i = 0; i < counties.size(); i++){
                                                                Country country 
    = (Country)counties.get(i);
                                                                
    String cid = country.getId();
                                                                out.print(
    "<option value="+ cid +">"+ country.getCountry() +"</option>");
                                                            }
                                                        }
                                                        
                                                    
    %>
                                                
    </select>
                                            
    </td>
                                            
    <td id="provincesTd">
                                                
    <select name="provinces" style="width:95px" onchange="changeCountry(this)">
                                                    
    <%
                                                    List
    <Province> provinces = (List<Province>)request.getAttribute("provinceLs");
                                                        
                                                        out.print(
    "<option value='0'>----請選擇----</option>");
                                                        
    if(provinces!=null || provinces.size()>0){
                                                            
    for (int i = 0; i < provinces.size(); i++){
                                                                Province province 
    = (Province)provinces.get(i);
                                                                
    String pid = province.getId();
                                                                out.print(
    "<option value="+ pid +">"+ province.getProvince() +"</option>");
                                                            }
                                                        }
                                                        
                                                    
    %>
                                                
    </select>
                                            
    </td>
                                            
    <td>
                                                
    <label>城市</label>
                                                
    <input type="text" id="city" name="city" style="width: 200px; height: 25px" onfocus="changeProvince(this)" />
                                                
    <input type="button" value="添加城市" style="width:80px; height: 25px" onclick="addCity()" />
                                            
    </td>
                                        
    </tr>
                                    
    </table>
                                
    </fieldset>
    changeCountry:
    function changeCountry(obj){
        
    //取得所選的國家
        var countryId = $("countries").value;
        
    if(countryId == 0){
            alert(
    "請選擇國家,如國家列表為空,請先添加國家,以便能準確添加市級地區");
            $(
    "countries").focus();
            
    return;
        }
        
    // 取得所選擇的省
        else{
            url
    ="ChangeCountry?countryId=";
            
    var countryId = $("countries").value;
            url
    =url+countryId;
            xmlHtpRq
    =ajaxObj=createAjaxObject();
            xmlHtpRq.open(
    "GET",url,true);
            xmlHtpRq.setRequestHeader(
    "Content-Type","text/html;charset=UTF-8");
            xmlHtpRq.onreadystatechange 
    =function(){OnStatusChange();}// 注冊回調函數,接收服務器的響應
            xmlHtpRq.send();
        }
        
    }
    OnStatusChange:
    function OnStatusChange(){    //回調函數,接收服務器的響應()
                if(ajaxObj.readyState==4){
                
    if (xmlHtpRq.status==200){            
                    
    var status = xmlHtpRq.responseXML.getElementsByTagName("status")[0].firstChild.data;
                    
                    
    if( status == "ok"){    

                        
    var subInnerHTML = xmlHtpRq.responseXML.getElementsByTagName("content")[0].xml;
                        
                        
    //alert(subInnerHTML);
                        
                        
    var classNameDiv = document.getElementById("provincesTd");
                        
                        
    var provinces = document.getElementById("provinces");
                        
                        document.getElementById(
    "provincesTd").removeChild(provinces);
                        
                        classNameDiv.innerHTML
    =subInnerHTML;
        
                    } 
                }
                
    else//頁面不正常
                    alert(xmlHtpRq.status);    // 輸出狀態碼
                    
                }       
    changeProvinces:
    function changeProvinces(obj,myarray){
        
    //取得省份數組
        var arr = myarray;
        
    var vform=obj.form;
        
    // 取得省份下拉框并清除原有選項
        var provinces=vform["provinces"];
        provinces.length
    =0;

        
    // 重新填充到省份下拉框
        for(var i=0;i<arr.length;i++){
            provinces[i
    +1]=new Option(arr[i],i);
        }    
    }
    ChangeCountryServlet:
    package com.runsky.action;

    import java.io.PrintWriter;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.runsky.domain.City;
    import com.runsky.domain.Country;
    import com.runsky.domain.Province;
    import com.runsky.service.BaseService;
    import com.runsky.util.StringFormatUtil;

    /**
     * 添加省份
     * 
     * 
    @author Ying-er
     * 
    @since 2010-1-14 下午03:34:36
     * 
    @version 1.00 Ying-er 創建 2010-1-14 下午03:34:36
     
    */
    public class ChangeCountryServlet extends HttpServlet {

        
    /**
         * 
         
    */
        
    private static final long serialVersionUID = 1343432526456565L;

        
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, java.io.IOException {
            
    // Setup Response
            response.setContentType("text/xml;charset=UTF-8");
            response.setHeader(
    "Cache-Control""no-cache");
            response.setCharacterEncoding(
    "UTF-8");

            
    // 取得輸入參數
            String countryId = StringFormatUtil
                    .getDecodeParamFromReq(
    "countryId", request);
            
    // System.out.println("------------------------------------------>"+str);
            BaseService<Province> pService = new BaseService<Province>();
            List
    <Province> provinceList = pService.getByField(Province.class"countryId", countryId);
            PrintWriter out 
    = response.getWriter();
            
                
    if(provinceList.size()<1 || provinceList == null){
                    out.println(
    "<response>");
                    out.println(
    "<status>ok</status>");
                    out.print(
    "<content>");
                    out.print(
    "<select name='provinces' id='provinces'>");
                    out.print(
    "<option value='0'> --    無數據   -- </option>");
                    out.print(
    "</select>");
                    out.print(
    "</content>");
                    out.println(
    "</response>");
                }
                
    else{
                    out.println(
    "<response>");
                    out.println(
    "<status>ok</status>");
                    out.print(
    "<content>");
                        out.print(
    "<select name='provinces' id='provinces'>");
                        out.print(
    "<option value='0'> -- 請選擇 -- </option>");
                        
    for(Province province : provinceList){
                            out.print(
    "<option value='" + province.getId() + "'>"
                                    
    + province.getProvince() + "</option>");
                        }
                        out.print(
    "</select>");
                    out.print(
    "</content>");
                    out.println(
    "</response>");
                }
                
            
        }

        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, java.io.IOException {
            doPost(request, response);
        }
    }





    posted on 2010-05-29 19:01 Ying-er 閱讀(508) 評論(1)  編輯  收藏

    評論:
    # re: 與數據庫交互的二級聯動 2010-05-30 09:51 | Ora
    不錯。  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲人成电影网站| 97免费人妻无码视频| 亚洲av无码专区在线观看下载| 亚洲成AV人片在线播放无码| 国产美女无遮挡免费视频| 亚洲免费电影网站| 99在线免费观看| 国产综合成人亚洲区| 国产色在线|亚洲| 久久精品国产亚洲AV网站| 亚洲欧洲日产国码一级毛片| 成全视频免费高清| 久久九九兔免费精品6| 好久久免费视频高清| 无码日韩人妻AV一区免费l| 亚洲kkk4444在线观看| 亚洲美女在线观看播放| 国产成A人亚洲精V品无码性色| 国产一区二区三区在线免费| 在线v片免费观看视频| 50岁老女人的毛片免费观看| 玖玖在线免费视频| 99免费精品视频| 香蕉免费看一区二区三区| 免费VA在线观看无码| 亚洲av无码有乱码在线观看| 亚洲熟妇AV日韩熟妇在线| 亚洲人成激情在线播放| 亚洲综合激情另类小说区| 久久亚洲精品无码AV红樱桃| 亚洲熟妇无码八AV在线播放| 一本色道久久综合亚洲精品高清| 免费播放春色aⅴ视频| 免费a级毛片视频| 免费大片在线观看网站| 伊在人亚洲香蕉精品区麻豆| 国产免费卡一卡三卡乱码| jjzz亚洲亚洲女人| 亚洲成a人一区二区三区| 亚洲精品无码永久在线观看| 亚洲国产综合久久天堂|