jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果(附源碼)
本文實(shí)例講述了jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果的方法。分享給大家供大家參考,具體如下:
在這里,用MySQL數(shù)據(jù)庫(kù)存儲(chǔ)了全國(guó)所有的省市縣地區(qū)信息(點(diǎn)擊此處下載源代碼)
使用過(guò)的jar包
google的Gson.jar
mysql-connector-java-5.1.13-bin.jar
將實(shí)驗(yàn)圖貼出來(lái):

顯示頁(yè)面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ū)三級(jí)聯(lián)動(dòng)下拉菜單</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="">請(qǐng)選擇</option>
</select>
區(qū)(縣):
<select name="district" id="district">
<option value="">請(qǐng)選擇</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("請(qǐng)選擇"));
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ù)庫(kù)交互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);
}
}
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- 簡(jiǎn)單實(shí)用jquery版三級(jí)聯(lián)動(dòng)select示例
- jquery+json 通用三級(jí)聯(lián)動(dòng)下拉列表
- 省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼
- jQuery select表單提交省市區(qū)城市三級(jí)聯(lián)動(dòng)核心代碼
- jQuery實(shí)現(xiàn)的省市縣三級(jí)聯(lián)動(dòng)菜單效果完整實(shí)例
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- 基于jQuery+JSON的省市二三級(jí)聯(lián)動(dòng)效果
- JSON+Jquery省市區(qū)三級(jí)聯(lián)動(dòng)
- jquery實(shí)現(xiàn)的省市區(qū)三級(jí)聯(lián)動(dòng)
- jQuery實(shí)現(xiàn)簡(jiǎn)單三級(jí)聯(lián)動(dòng)效果
相關(guān)文章
jQuery-Citys省市區(qū)三級(jí)菜單聯(lián)動(dòng)插件使用詳解
這篇文章主要為大家詳細(xì)介紹了jQuery-Citys省市區(qū)三級(jí)菜單聯(lián)動(dòng)插件使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
jQuery特殊符號(hào)轉(zhuǎn)義的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇jQuery特殊符號(hào)轉(zhuǎn)義的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
jQuery Validate驗(yàn)證框架詳解(推薦)
jQuery Validate 插件為表單提供了強(qiáng)大的驗(yàn)證功能,讓客戶(hù)端表單驗(yàn)證變得更簡(jiǎn)單,同時(shí)提供了大量的定制選項(xiàng),滿(mǎn)足應(yīng)用程序各種需求。有興趣的可以了解一下。2016-12-12
jQuery實(shí)現(xiàn)帶幻燈的tab滑動(dòng)切換風(fēng)格菜單代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)帶幻燈的tab滑動(dòng)切換風(fēng)格菜單代碼,可實(shí)現(xiàn)點(diǎn)擊菜單項(xiàng)進(jìn)行對(duì)應(yīng)內(nèi)容的滑動(dòng)切換功能,涉及jquery鼠標(biāo)事件及頁(yè)面元素屬性的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2015-08-08
jquery ajax 請(qǐng)求小技巧實(shí)例分析
這篇文章主要介紹了jquery ajax 請(qǐng)求小技巧,結(jié)合實(shí)例形式分析了jquery ajax請(qǐng)求操作相關(guān)配置與使用技巧,需要的朋友可以參考下2019-11-11
jquery實(shí)現(xiàn)可關(guān)閉的倒計(jì)時(shí)廣告特效代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)可關(guān)閉的倒計(jì)時(shí)廣告特效代碼,涉及jquery計(jì)時(shí)器及鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
jQuery $.get 的妙用 訪問(wèn)本地文本文件
當(dāng)頁(yè)面文件.html作為本地文件打開(kāi)(即IE路徑為file:///開(kāi)頭的)的時(shí)候,需要訪問(wèn)本地文本文件的內(nèi)容時(shí)2012-07-07

