ajax實現(xiàn)城市三級聯(lián)動
更新時間:2021年10月18日 10:04:58 作者:土豆才不土
這篇文章主要為大家詳細介紹了ajax實現(xiàn)城市三級聯(lián)動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了ajax實現(xiàn)城市三級聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下
在準備好服務(wù)器后
html部分
<div>
<select name="" id="province">
<option value="">請選擇省份</option>
</select>
<select name="" id="city">
<option value="">請選擇城市</option>
</select>
<select name="" id="district">
<option value="">請選擇區(qū)域</option>
</select>
</div>
樣式部分
<style>
div {
text-align: center;
}
select {
width: 150px;
height: 30px;
}
</style>
js部分
<script>
var a = 0;
var b = 0;
var d=null;
$.ajax({
type:'get',
url:'http://127.0.0.1:6562/0929/area-json.js',
success: function(data){
d = JSON.parse(data.slice(10,-1)); //獲取json數(shù)據(jù)并轉(zhuǎn)化為數(shù)組數(shù)據(jù)
$.each(d,function(index,ele){
$('<option value = ""></option>').appendTo('#province').text(ele.name);//把省的數(shù)據(jù)插入列表中
})
}
})
$('#province').on('change',function(e){ //當省變化時
$.ajax({
type:'get',
url:'http://127.0.0.1:6562/0929/area-json.js',
success: function(data){
d=JSON.parse(data.slice(10,-1));
a = e.target.selectedIndex - 1; //當前下拉列表下標
if(a == -1){
$('#city').html('<option value="">請選擇城市</option>');
$('#district').html('<option value="">請選擇區(qū)域</option>');
}else{
$('#city').html('<option value="">請選擇城市</option>');
$('#district').html('<option value="">請選擇區(qū)域</option>');
if(d[a].children){
$.each(d[a].children,function(index,ele){
$('<option value = ""></option>').appendTo('#city').text(ele.name);
})
}
}
}
})
})
$('#city').on('change',function(e){ //當市變化時
$.ajax({
type:'get',
url:'http://127.0.0.1:6562/0929/area-json.js',
success: function(data){
d=JSON.parse(data.slice(10,-1));
b = e.target.selectedIndex - 1;
if(b == -1){
$('#district').html('<option value="">請選擇區(qū)域</option>');
}else{
$('#district').html('<option value="">請選擇區(qū)域</option>');
if(d[a].children[b].children){
$.each(d[a].children[b].children,function(index,ele){
$('<option value = ""></option>').appendTo('#district').text(ele.name);
})
}
}
}
})
})
</script>
全部代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.3/jquery.js"></script>
<style>
div {
text-align: center;
}
select {
width: 150px;
height: 30px;
}
</style>
</head>
<body>
<div>
<select name="" id="province">
<option value="">請選擇省份</option>
</select>
<select name="" id="city">
<option value="">請選擇城市</option>
</select>
<select name="" id="district">
<option value="">請選擇區(qū)域</option>
</select>
</div>
<script>
var a = 0;
var b = 0;
var d=null;
$.ajax({
type:'get',
url:'http://127.0.0.1:6562/0929/area-json.js',
success: function(data){
d = JSON.parse(data.slice(10,-1));
$.each(d,function(index,ele){
$('<option value = ""></option>').appendTo('#province').text(ele.name);
})
}
})
$('#province').on('change',function(e){
$.ajax({
type:'get',
url:'http://127.0.0.1:6562/0929/area-json.js',
success: function(data){
d=JSON.parse(data.slice(10,-1));
a = e.target.selectedIndex - 1;
if(a == -1){
$('#city').html('<option value="">請選擇城市</option>');
$('#district').html('<option value="">請選擇區(qū)域</option>');
}else{
$('#city').html('<option value="">請選擇城市</option>');
$('#district').html('<option value="">請選擇區(qū)域</option>');
if(d[a].children){
$.each(d[a].children,function(index,ele){
$('<option value = ""></option>').appendTo('#city').text(ele.name);
})
}
}
}
})
})
$('#city').on('change',function(e){
$.ajax({
type:'get',
url:'http://127.0.0.1:6562/0929/area-json.js',
success: function(data){
d=JSON.parse(data.slice(10,-1));
b = e.target.selectedIndex - 1;
if(b == -1){
$('#district').html('<option value="">請選擇區(qū)域</option>');
}else{
$('#district').html('<option value="">請選擇區(qū)域</option>');
if(d[a].children[b].children){
$.each(d[a].children[b].children,function(index,ele){
$('<option value = ""></option>').appendTo('#district').text(ele.name);
})
}
}
}
})
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動
- AJAX省市區(qū)三級聯(lián)動下拉菜單(java版)
- 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)動
- ajax實現(xiàn)三級聯(lián)動的基本方法
- AJAX和WebService實現(xiàn)省市縣三級聯(lián)動具體代碼
相關(guān)文章
ASP小偷程序如何利用XMLHTTP實現(xiàn)表單的提交
ASP小偷程序如何利用XMLHTTP實現(xiàn)表單的提交...2006-07-07
JQuery+Ajax+Struts2+Hibernate框架整合實現(xiàn)完整的登錄注冊
最近沒有項目做,閑著沒事,看到一個書城的網(wǎng)站很好,于是就是仿了一個,把里面的ui直接拿過來用,前端后端自己寫的,目前大部分的功能已經(jīng)實現(xiàn)了。2015-10-10
Ajax技術(shù)(WEB無刷新提交數(shù)據(jù))-
Ajax技術(shù)(WEB無刷新提交數(shù)據(jù))-...2006-07-07

