jQuery+jsp實現(xiàn)省市縣三級聯(lián)動效果(附源碼)
本文實例講述了jQuery+jsp實現(xiàn)省市縣三級聯(lián)動效果的方法。分享給大家供大家參考,具體如下:
在這里,用MySQL數(shù)據(jù)庫存儲了全國所有的省市縣地區(qū)信息(點擊此處下載源代碼)
使用過的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>省市區(qū)三級聯(lián)動下拉菜單</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>
區(qū)(縣):
<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>
數(shù)據(jù)庫交互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程序設(shè)計有所幫助。
- 簡單實用jquery版三級聯(lián)動select示例
- jquery+json 通用三級聯(lián)動下拉列表
- 省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼
- jQuery select表單提交省市區(qū)城市三級聯(lián)動核心代碼
- jQuery實現(xiàn)的省市縣三級聯(lián)動菜單效果完整實例
- asp.net省市三級聯(lián)動的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- 基于jQuery+JSON的省市二三級聯(lián)動效果
- JSON+Jquery省市區(qū)三級聯(lián)動
- jquery實現(xiàn)的省市區(qū)三級聯(lián)動
- jQuery實現(xiàn)簡單三級聯(lián)動效果
相關(guān)文章
jquery $(this).attr $(this).val方法使用介紹
$(this).attr(key); 獲取節(jié)點屬性名的值,相當(dāng)于getAttribute(key)方法,本文整理了一些相關(guān)的示例,感興趣的朋友可以參考下2013-10-10
基于jquery實現(xiàn)表格內(nèi)容篩選功能實例解析
對于表格來說,當(dāng)數(shù)據(jù)比較多的時候,我們無法一頁一頁的查找,這樣我們就可以進行篩選操作,這篇文章主要為大家詳細介紹了基于jquery實現(xiàn)表格內(nèi)容篩選功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
詳解ajax的data參數(shù)錯誤導(dǎo)致頁面崩潰
本篇文章給大家詳細分析了ajax的data參數(shù)錯誤導(dǎo)致頁面崩潰的原因以及解決辦法,有需要的朋友參考學(xué)習(xí)下。2018-04-04
jquery清空textarea等輸入框?qū)崿F(xiàn)代碼
jquery清空textarea等輸入框在工作中很常見,接下來的代碼簡單實用,感興趣的朋友可以參考下哈,希望對你有所幫助2013-04-04

