jQuery+jsp實現省市縣三級聯動效果(附源碼)_jquery

來源:互聯網
上載者:User

本文執行個體講述了jQuery+jsp實現省市縣三級聯動效果的方法。分享給大家供大家參考,具體如下:

在這裡,用MySQL資料庫儲存了全國所有的省市縣地區資訊(點擊此處下載原始碼)

使用過的jar包

google的Gson.jar
mysql-connector-java-5.1.13-bin.jar

將實驗圖貼出來:

顯示頁面index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>">  <title>省市區三級聯動下拉式功能表</title> <script type="text/javascript" src="<%=path %>/js/jquery/jquery-1.7.min.js"></script> <script type="text/javascript" src="<%=path %>/js/json/json-minified.js"></script> </head> <body> <table> <tr> <td> 省份: <select name="province" id="province" onchange="onSelectChange(this,'city');"></select> 城市: <select name="city" id="city" onchange="onSelectChange(this,'district');">  <option value="">請選擇</option> </select> 區(縣): <select name="district" id="district">  <option value="">請選擇</option> </select> </td> </tr> </table> </body></html><script type="text/javascript">function onSelectChange(obj,toSelId){ setSelect(obj.value,toSelId);}function setSelect(fromSelVal,toSelId){ //alert(document.getElementById("province").selectedIndex); document.getElementById(toSelId).innerHTML=""; jQuery.ajax({  url: "<%=path%>/getDropdownDataServlet",  cache: false,  data:"parentId="+fromSelVal,  success: function(data){  createSelectObj(data,toSelId);  } });}function createSelectObj(data,toSelId){ var arr = jsonParse(data); if(arr != null && arr.length>0){  var obj = document.getElementById(toSelId);  obj.innerHTML="";  var nullOp = document.createElement("option");  nullOp.setAttribute("value","");  nullOp.appendChild(document.createTextNode("請選擇"));  obj.appendChild(nullOp);  for(var o in arr){   var op = document.createElement("option");   op.setAttribute("value",arr[o].id);   //op.text=arr[o].name;//這一句在ie下不起作用,用下面這一句或者innerHTML   op.appendChild(document.createTextNode(arr[o].name));   obj.appendChild(op);  } }}setSelect('1','province');</script>

資料庫互動GetDropdownDataServlet

public class GetDropdownDataServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)   throws IOException, ServletException {  doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)   throws IOException, ServletException {  String parentId = request.getParameter("parentId");  if (parentId == null || parentId == "") {   parentId = "0";  }  Connection conn = null;  String json = "";  try {   Class.forName("com.mysql.jdbc.Driver");   conn = DriverManager.getConnection("jdbc:mysql://localhost/dropdown",     "root", "root");   Statement stat = conn.createStatement();   ResultSet rs = stat     .executeQuery("select region_id,region_name from region where parent_id = "       + parentId);   ArrayList rsList = new ArrayList();   Map map = null;   while (rs.next()) {    map = new HashMap();    map.put("id", rs.getInt(1));    map.put("name", rs.getString(2));    rsList.add(map);   }   rs = null;   Gson gson = new Gson();   json = gson.toJson(rsList);   System.out.println("json=" + json);  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  } finally {   if (conn != null) {    try {     conn.close();    } catch (SQLException e) {     e.printStackTrace();    }   }  }  response.setCharacterEncoding("UTF-8");  response.getWriter().print(json); }}

希望本文所述對大家jQuery程式設計有所協助。

相關文章

聯繫我們

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