Ajax實現(xiàn)三級聯(lián)動效果
本文實例為大家分享了Ajax實現(xiàn)三級聯(lián)動效果的具體代碼,供大家參考,具體內(nèi)容如下
一、導(dǎo)入數(shù)據(jù)表和gson.jar
該表包括了中國所有的省、市、縣、區(qū),它們之間通過parentid關(guān)聯(lián)。

二、后端代碼
由于每一級的數(shù)據(jù)都是根據(jù)上一級的id查詢而來,邏輯十分相似,故我們只需要一個接口就可以完成三級甚至更多級的聯(lián)動,在這個案例中我們的核心查詢就是select * from area where parentid=#{pid}
entity
package com.codeXie.entity;
import java.io.Serializable;
public class Area implements Serializable {
private String areaid;
private String areaname;
private String parentid;
private Integer arealevel;
private Integer status;
public Area() {
}
public Area(String areaid, String areaname, String parentid, Integer arealevel, Integer status) {
this.areaid = areaid;
this.areaname = areaname;
this.parentid = parentid;
this.arealevel = arealevel;
this.status = status;
}
.......省略了對各屬性的set、get
}
mapper
public interface AreaMapper {
@Select("select * from area where parentid=#{pid}")
List<Area> selectMore(Integer pid);
}
service
public interface AreaService {
List<Area> findCity(int pid);
}
servlet
@WebServlet("/AreaServlet")
public class AreaServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String pid = req.getParameter("pid");
AreaServiceImpl service = new AreaServiceImpl();
List<Area> areas = service.findCity(Integer.parseInt(pid));
String json = new Gson().toJson(areas);
resp.getWriter().print(json);
}
}
三、前端代碼
<script src="js/jquery.js"></script>
<script>
function produceOption(id,list){
console.log(list)
$(id).empty()
$(list).each((index,item)=>{
$(id).append("<option value="+item.areaid+">"+item.areaname+"</option>")
})
}
$(()=>{
$.ajax({
url:"AreaServlet",
method:"post",
data:{pid:0},
dataType:"json",
success: function(res) {
produceOption("#proviance",res)
$("#proviance").prepend("<option selected='selected'>請選擇省份</option>")
}
})
$("#proviance").change(function(){
var pid = $(this).prop("value")
$.ajax({
url:"AreaServlet",
method:"post",
data:{pid:pid},
dataType:"json",
success: function(res) {
produceOption("#city",res)
$("#city").prepend("<option selected='selected'>請選擇城市</option>")
}
})
})
$("#city").on("change",function(){
var pid = $(this).prop("value")
$.ajax({
url:"AreaServlet",
method:"post",
data:{pid:pid},
dataType:"json",
success: function(res) {
produceOption("#country",res)
}
})
})
})
</script>
</head>
<body>
<h2>三級聯(lián)動</h2>
<hr/>
<select name="pro" id="proviance">
<option>選擇省份</option>
</select>
<select name="city" id="city">
<option>選擇城市</option>
</select>
<select name="country" id="country">
<option>請選擇區(qū)域</option>
</select>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動
- AJAX省市區(qū)三級聯(lián)動下拉菜單(java版)
- asp.net省市三級聯(lián)動的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- ajax三級聯(lián)動下拉菜單效果
- jQuery ajax實現(xiàn)省市縣三級聯(lián)動
- ajax三級聯(lián)動的實現(xiàn)方法
- jquery+ajax實現(xiàn)省市區(qū)三級聯(lián)動效果簡單示例
- jquery+ajax實現(xiàn)省市區(qū)三級聯(lián)動(封裝和不封裝兩種方式)
- ajax實現(xiàn)無刷新省市縣三級聯(lián)動
- 使用PHP+MySql+Ajax+jQuery實現(xiàn)省市區(qū)三級聯(lián)動功能示例
相關(guān)文章
[js]輕便的XMLHttpRequest應(yīng)用函數(shù):downloadUrl()
[js]輕便的XMLHttpRequest應(yīng)用函數(shù):downloadUrl()...2007-04-04
關(guān)于前端ajax請求的優(yōu)雅方案(http客戶端為axios)
這篇文章主要給大家介紹了關(guān)于前端ajax請求的優(yōu)雅方案,本文http客戶端為axios,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Axios和Ajax的區(qū)別是什么(詳細(xì)介紹)
ajax技術(shù)實現(xiàn)了局部數(shù)據(jù)的刷新,axios實現(xiàn)了對ajax的封裝,axios有的ajax都有,ajax有的axios不一定有,總結(jié)一句話就是axios是ajax,ajax不止axios,本文對Axios和Ajax的區(qū)別是什么給大家講解的非常詳細(xì),需要的朋友一起看看吧2023-10-10

