您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用jquery怎么實現戶籍地選擇下拉框,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
data.js
var data = [{ provname: '浙江省', provId: 1, citys: [{ cityname: "杭州市", cityId: 101, areas: [{ areaname: "杭州一區", areaId: 1011 }, { areaname: "杭州二區", areaId: 1012 } ] }, { cityname: "溫州市", cityId: 102, areas: [{ areaname: '溫州一區', areaId: 1021 }, { areaname: '溫州二區', areaId: 1022 }] }, { cityname: "寧波市", cityId: 103, areas: [{ areaname: '寧波一區', areaId: 1031 }, { areaname: '寧波二區', areaId: 1032 }] }, { cityname: "紹興市", cityId: 104, areas: [{ areaname: '紹興一區', areaId: 1041 }, { areaname: '紹興二區', areaId: 1042 }] }] }, { provname: '山東省', provId: 2, citys: [{ cityname: "濟南市", cityId: 201, areas: [{ areaname: "濟南一區", areaId: 2011 }, { areaname: "濟南二區", areaId: 2012 } ] }, { cityname: "青島市", cityId: 202, areas: [{ areaname: '青島一區', areaId: 2021 }, { areaname: '青島二區', areaId: 2022 }] }, { cityname: "濟寧市", cityId: 203, areas: [{ areaname: '濟寧一區', areaId: 2031 }, { areaname: '濟寧二區', areaId: 2032 }] }, { cityname: "濰坊市", cityId: 204, areas: [{ areaname: '濰坊一區', areaId: 2041 }, { areaname: '濰坊二區', areaId: 2042 }] }] }, { provname: '廣東省', provId: 3, citys: [{ cityname: "廣州市", cityId: 301, areas: [{ areaname: "廣州一區", areaId: 3011 }, { areaname: "廣州二區", areaId: 3012 } ] }, { cityname: "潮陽市", cityId: 302, areas: [{ areaname: '潮陽一區', areaId: 3021 }, { areaname: '潮陽二區', areaId: 3022 }] }, { cityname: "澄海市", cityId: 303, areas: [{ areaname: '澄海一區', areaId: 3031 }, { areaname: '澄海二區', areaId: 3032 }] }, { cityname: "潮州市", cityId: 304, areas: [{ areaname: '潮州一區', areaId: 3041 }, { areaname: '潮州二區', areaId: 3042 }] }] }, { provname: '甘肅省', provId: 4, citys: [{ cityname: "蘭州市", cityId: 401, areas: [{ areaname: "蘭州一區", areaId: 4011 }, { areaname: "蘭州二區", areaId: 4012 } ] }, { cityname: "白銀市", cityId: 402, areas: [{ areaname: '白銀一區', areaId: 4021 }, { areaname: '白銀二區', areaId: 4022 }] }, { cityname: "敦煌市", cityId: 403, areas: [{ areaname: '敦煌一區', areaId: 4031 }, { areaname: '敦煌二區', areaId: 4032 }] }, { cityname: "定西市", cityId: 404, areas: [{ areaname: '定西一區', areaId: 4041 }, { areaname: '定西二區', areaId: 4042 }] }] } ]
demo.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="js/jquery.min.js"></script> <script src="js/data.js"></script> </head> <body> <!-- 先添加三個下拉列表 --> <select name="prov" id="prov"> </select> <select name="city" id="city"> </select> <select name="area" id="area"> </select> <script> var $prov=$("#prov") var $city=$("#city") var $area=$("#area") $(function(){ //頁面加載完成后觸發 $.each(data,function(i,e){ $prov.append('<option value="'+e.provId+'">'+e.provname+'</option>') //在$obj的末尾追加子元素newObj }) $prov.prepend('<option value="" selected>請選擇</option>'); //當省名被選擇時,觸發以下事件 $prov.on("change",function(){ //遍歷省 $.each(data,function(i,e){ if($prov.val()==e.provId){ //遍歷市 $city.html('<option value="">請選擇</option>');//用來清空之前選擇的市 $.each(e.citys,function(i,e2){ $city.append('<option value="'+e2.cityId+'">'+e2.cityname+'</option>'); }) } }) }) //當市名被選擇時,觸發以下事件 $city.on("change",function(){ //遍歷省 $.each(data,function(i,e){ if($prov.val()==e.provId){ $.each(e.citys,function(i,e2){ if($city.val()==e2.cityId){ $area.html('<option value="">請選擇</option>'); $.each(e2.areas,function(i,e3){ $area.append('<option value="'+e3.areaId+'">'+e3.areaname+'</option>'); }) } }) } }) }) }) </script> </body> </html>
看完上述內容,你們對使用jquery怎么實現戶籍地選擇下拉框有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。