您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)使用JavaScript怎么實(shí)現(xiàn)一個(gè)省市區(qū)三級選擇聯(lián)級,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.可以使網(wǎng)頁具有交互性,例如響應(yīng)用戶點(diǎn)擊,給用戶提供更好的體驗(yàn)。 2.可以處理表單,檢驗(yàn)用戶的輸入,并提供及時(shí)反饋節(jié)省用戶時(shí)間。 3.可以根據(jù)用戶的操作,動態(tài)的創(chuàng)建頁面。 4使用JavaScript可以通過設(shè)置cookie存儲在瀏覽器上的一些臨時(shí)信息。
代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <select id="province"> <option>---請選擇---</option> </select> <select id="city"> <option>---請選擇---</option> </select> <select id="area"> <option>---請選擇---</option> </select> <script src="addr.js"></script> <script src="../lib/jquery-3.3.1.js"></script> <script> var pro = []; $(function (){ $.each(temp,function (){ $("#province").append("<option>"+$(this)[0].label+"</option>"); }); $("#province").on("change",function (){ $("#city").html("<option>"+"---請選擇---"+"</option>"); $("#area").html("<option>"+"---請選擇---"+"</option>") var select_pro = $(this).val(); $.each(temp,function (index,element){ if (element.label == select_pro){ var city = element.children; for (let i = 0; i < city.length ; i++) { $("#city").append("<option>"+city[i].label+"</option>"); } $("#city").on('change',function () { $("#area").html("<option>"+"---請選擇---"+"</option>"); var select_city = $(this).val(); for (var i=0;i < city.length ; i++) { console.log(city[i].label); if (city[i].label == select_city) { var area = city[i].children; for (var i=0;i < area.length ; i++) { $("#area").append("<option>"+area[i].label+"</option>"); } } } }); } }); }); }); </script> </body> </html>
上述就是小編為大家分享的使用JavaScript怎么實(shí)現(xiàn)一個(gè)省市區(qū)三級選擇聯(lián)級了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。