ajax實現(xiàn)城市三級聯(lián)動
更新時間:2021年10月18日 10:04:58 作者:土豆才不土
這篇文章主要為大家詳細(xì)介紹了ajax實現(xiàn)城市三級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了ajax實現(xiàn)城市三級聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下
在準(zhǔn)備好服務(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){ //當(dāng)省變化時 $.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; //當(dāng)前下拉列表下標(biāo) 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){ //當(dāng)市變化時 $.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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 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.NET與Ajax的實現(xiàn)方式小總結(jié)
Ajax 應(yīng)該不是一項技術(shù),是一種思想而已,跟 ASP.NET 以及其它 Web 開發(fā)語言沒有什么太大關(guān)系,這里只是談?wù)?ASP.NET 中目前使用的 Ajax 技術(shù)以及其它一些實現(xiàn) Ajax 的優(yōu)秀框架。感興趣的朋友跟著小編一起學(xué)習(xí)asp.net與ajax的實現(xiàn)方式2015-09-09用AJAX技術(shù)實現(xiàn)在自己Blog上聚合并顯示朋友Blog的最新文章
在自己Blog上聚合并顯示朋友Blog的最新文章,這樣方便自己及時了解朋友的消息,另外,也方便訪問者找到和本Blog相關(guān)的blog和文章2014-05-05使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能
這篇文章主要介紹了使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能,很不錯的嘗試,需要的朋友可以參考下2014-08-08AJAX和三層架構(gòu)實現(xiàn)分頁功能具體思路及代碼
本文涉及到AJAX和三層架構(gòu)方面的知識,在學(xué)習(xí)分頁的同時也鞏固了一下它們的相關(guān)知識,適合初學(xué)者的你2013-05-05