ajax實(shí)現(xiàn)城市三級(jí)聯(lián)動(dòng)
本文實(shí)例為大家分享了ajax實(shí)現(xiàn)城市三級(jí)聯(lián)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
在準(zhǔn)備好服務(wù)器后
html部分
<div> <select name="" id="province"> <option value="">請(qǐng)選擇省份</option> </select> <select name="" id="city"> <option value="">請(qǐng)選擇城市</option> </select> <select name="" id="district"> <option value="">請(qǐng)選擇區(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)省變化時(shí) $.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="">請(qǐng)選擇城市</option>'); $('#district').html('<option value="">請(qǐng)選擇區(qū)域</option>'); }else{ $('#city').html('<option value="">請(qǐng)選擇城市</option>'); $('#district').html('<option value="">請(qǐng)選擇區(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)市變化時(shí) $.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ǐng)選擇區(qū)域</option>'); }else{ $('#district').html('<option value="">請(qǐng)選擇區(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="">請(qǐng)選擇省份</option> </select> <select name="" id="city"> <option value="">請(qǐng)選擇城市</option> </select> <select name="" id="district"> <option value="">請(qǐng)選擇區(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="">請(qǐng)選擇城市</option>'); $('#district').html('<option value="">請(qǐng)選擇區(qū)域</option>'); }else{ $('#city').html('<option value="">請(qǐng)選擇城市</option>'); $('#district').html('<option value="">請(qǐng)選擇區(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ǐng)選擇區(qū)域</option>'); }else{ $('#district').html('<option value="">請(qǐng)選擇區(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP+Mysql+Ajax+JS實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)
- ajax三級(jí)聯(lián)動(dòng)下拉菜單效果
- jQuery ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
- ajax三級(jí)聯(lián)動(dòng)的實(shí)現(xiàn)方法
- jquery+ajax實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)效果簡(jiǎn)單示例
- jquery+ajax實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)(封裝和不封裝兩種方式)
- ajax實(shí)現(xiàn)無(wú)刷新省市縣三級(jí)聯(lián)動(dòng)
- ajax實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的基本方法
- AJAX和WebService實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)具體代碼
相關(guān)文章
ASP.NET與Ajax的實(shí)現(xiàn)方式小總結(jié)
Ajax 應(yīng)該不是一項(xiàng)技術(shù),是一種思想而已,跟 ASP.NET 以及其它 Web 開(kāi)發(fā)語(yǔ)言沒(méi)有什么太大關(guān)系,這里只是談?wù)?ASP.NET 中目前使用的 Ajax 技術(shù)以及其它一些實(shí)現(xiàn) Ajax 的優(yōu)秀框架。感興趣的朋友跟著小編一起學(xué)習(xí)asp.net與ajax的實(shí)現(xiàn)方式2015-09-09用AJAX技術(shù)實(shí)現(xiàn)在自己Blog上聚合并顯示朋友Blog的最新文章
在自己Blog上聚合并顯示朋友Blog的最新文章,這樣方便自己及時(shí)了解朋友的消息,另外,也方便訪(fǎng)問(wèn)者找到和本Blog相關(guān)的blog和文章2014-05-05ajax實(shí)現(xiàn)分頁(yè)查詢(xún)功能
這篇文章主要為大家詳細(xì)介紹了ajax實(shí)現(xiàn)分頁(yè)查詢(xún)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05使用Ajax技術(shù)通過(guò)XMLHttpRequest對(duì)象完成首頁(yè)登錄功能
這篇文章主要介紹了使用Ajax技術(shù)通過(guò)XMLHttpRequest對(duì)象完成首頁(yè)登錄功能,很不錯(cuò)的嘗試,需要的朋友可以參考下2014-08-08AJAX和三層架構(gòu)實(shí)現(xiàn)分頁(yè)功能具體思路及代碼
本文涉及到AJAX和三層架構(gòu)方面的知識(shí),在學(xué)習(xí)分頁(yè)的同時(shí)也鞏固了一下它們的相關(guān)知識(shí),適合初學(xué)者的你2013-05-05AJAX javascript的跨域訪(fǎng)問(wèn)執(zhí)行
在網(wǎng)站調(diào)用加載評(píng)論等信息的時(shí)候遇到了不同域名間javascript不能執(zhí)行的問(wèn)題,一直都在加載中顯示不出來(lái),而換個(gè)網(wǎng)址訪(fǎng)問(wèn)的話(huà)就能正確顯示,一直沒(méi)去注意瀏覽器提示的錯(cuò)誤信息:2008-04-04ajax無(wú)刷新分頁(yè)的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了ajax無(wú)刷新分頁(yè)的簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05ajax異步實(shí)現(xiàn)文件分片上傳實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于ajax異步實(shí)現(xiàn)文件分片上傳的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11AJAX開(kāi)發(fā)簡(jiǎn)略 (第二部分)
這篇文章主要給大家介紹了關(guān)于AJAX開(kāi)發(fā)簡(jiǎn)略 (第二部分)的相關(guān)資料,需要的朋友可以參考下2006-12-12