標籤: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 非同步實現省市聯動