全國省市聯動 Ajax非同步提交

來源:互聯網
上載者:User

Js 代碼:

<script type="text/javascript" language="javascript">
function getXmlHttp(){
    var http_request = false;
    if (window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType){
            http_request.overrideMimeType('text/xml');
 }
   }else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e) {
            }
        }
    }
    if (!http_request){
        alert('<%=mess%>');
        return false;
    }
    return http_request;
}

function getCity(value)
{   
 var ul="AjaxGetCityByProId?proId="+value;     
    req = getXmlHttp();
    req.onreadystatechange = result1;
    req.open("GET",ul,true);
    req.setRequestHeader('If-Modified-Since', '0');
    req.send(null);
}

function result1(){  
     if(req.readyState == 4 && req.status == 200)
     { 
      var citys = req.responseText.toString();
     document.getElementById("divCity").innerHTML=citys;
    }     
}
</script>

 

 

部分html代碼:

<TR bgColor=#AD8B4D>
                <TD align=right width="97">
                 <bean:message key="regist.province" />
                </TD>
                <TD width="387" align="left">
                 <select name="province" id="province" style="width: 140px;" class=input
                 onChange="getCity(this.options[this.options.selectedIndex].value);">
                  <option>
                   <bean:message key="regist.select" />
                  </option>
                  <%
                   Province pr = null;
                   for (int i = 0; i < allPros.size(); i++) {
                    pr = (Province) allPros.get(i);
                     %> 
                     <option value="<%=pr.getProId() %>">
                   <%=pr.getProName() %>
                  </option> 
                  <%
                   }
                  %>
                 </select>
                </TD>
               </TR>
                                   
               <TR bgColor=#AD8B4D>
                <TD align=right width="97">
                 <bean:message key="regist.city" />
                </TD>
                <TD width="387" align="left">
                 <span id="divCity"></span>                                 
                </TD>
               </TR>

 

部分 servlet代碼:

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String path = request.getContextPath();
  response.setContentType("text/xml; charset=GBK");
  PrintWriter out = response.getWriter();

  String proIds = request.getParameter("proId");
  Long proId = Long.parseLong(proIds);

  ServletContext application = request.getSession().getServletContext();
  WebApplicationContext webContext = WebApplicationContextUtils
    .getRequiredWebApplicationContext(application);
  UserService users = (UserService) webContext.getBean("userService");

  List allCity = null;
  try {
   allCity = users.getAllCityByProId(proId);
   City city = null;

   StringBuffer cityStr = new StringBuffer();
   cityStr.append("<select name=/"city/">");
   for (int i = 0; i < allCity.size(); i++) {
    city = (City) allCity.get(i);
    cityStr.append("  <option value='" + city.getCityId() + "'>")
      .append(city.getCityName()).append("</option>");
   }
   cityStr.append("</select>");
   out.print(cityStr);
  } catch (Exception e) {
   response.sendRedirect(path + "/error.jsp");
  }
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.