java 非同步實現省市聯動

來源:互聯網
上載者:User

標籤:jquer   bin   value   list   ring   font   change   size   遍曆   

1.

省份:<select id="provinceSelect"></select>城市:<select id="citySelect"></select>

 2.get,post請求都可

public String findProvinces(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {List<Province> list=dao.findProvinces();String jsonString=JSONArray.fromObject(list).toString();response.getWriter().write(jsonString);return null;}public String findCitysbyPid(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String pId=request.getParameter("pId");int provinceId=Integer.parseInt(pId);List<City> list=dao.findCitysByPId(provinceId);String jsonString=JSONArray.fromObject(list).toString();response.getWriter().write(jsonString);return null;}

  3.

  function initProvinceSelect()  {   $.getJSON("<c:url value=‘ProvinceCityServlet‘/>",{"method":"findProvinces"},function(data){//另一種遍曆方式  還有each方式遍曆 但在這裡不常用   常用在遍曆jquery元素集合上for(var i = 0; i < data.length; i++){  $("#provinceSelect").append("<option value=‘"+data[i].pid+"‘>"+data[i].name+"</option>")}  bindCitySelectChange();//注意綁定事件的順序  總是放在依賴控制項載入完後面 因為是非同步載入    //初始化   很重要 一般是想不到的  $("#provinceSelect").change();  });  }

  4.

  function bindCitySelectChange()  {  $("#provinceSelect").change(function(){  var pId=$(this).val();//擷取select選中項  很重要  $("#citySelect").empty();  //post get都行  $.post("<c:url value=‘ProvinceCityServlet‘/>",{"method":"findCitysbyPid","pId":pId},function(data){  for(var key in data)  {  $("#citySelect").append("<option>"+data[key].name+"</option>");  }    },"json");  });  }

  

java 非同步實現省市聯動

聯繫我們

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