您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用JavaScript怎么實現一個級聯算法,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
具體如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>LinkDemo</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> </script> <script type="text/javascript"> var shengshiString = "1_山東省-11_德州市+12_青島市+13_濟南市~2_河北省-21_邯鄲市*211_臨漳縣/212_磁縣+22_石家莊市*221_定縣/222_寧縣+23_保定市"; $(document).ready(function(){ init(); }); function init(){ var shengArray = shengshiString.split("~"); for (var i = 0; i < shengArray.length; i++) { var sheng = shengArray[i].split("-")[0]; var shi = shengArray[i].split("-")[1]; var shengid = sheng.split("_")[0]; var shengName = sheng.split("_")[1]; var Newoption = $("<option></option>"); $(Newoption).val(shengid); $(Newoption).text(shengName); $("#sheng").append(Newoption); if (i == 0) { shengchange(shengid); } } } function shengchange(shengidparam){ $("#shi").empty(); var shengArray = shengshiString.split("~"); for (var i = 0; i < shengArray.length; i++) { var shengString = shengArray[i].split("-")[0]; var shiString = shengArray[i].split("-")[1]; var shengId = shengString.split("_")[0]; if (shengidparam == shengId) { var shiArray = shiString.split("+"); for (var j = 0; j < shiArray.length; j++) { var shistring = shiArray[j].split("*")[0]; var shi = shistring; var shiId = shi.split("_")[0]; var shiName = shi.split("_")[1]; var Newoption = $("<option></option>"); $(Newoption).val(shiId); $(Newoption).text(shiName); $("#shi").append(Newoption); if (shiName == "邯鄲市" || shiName == "石家莊市") { shichange(shiId); } } } } } function shichange(shiidparam){ $("#xian").empty(); var shengArray = shengshiString.split("~"); for (var i = 0; i < shengArray.length; i++) { var shengString = shengArray[i].split("-")[0]; var shiString = shengArray[i].split("-")[1]; var shengId = shengString.split("_")[0]; var shiArray = shiString.split("+"); for (var j = 0; j < shiArray.length; j++) { var shistring = shiArray[j].split("*")[0]; var xianarray = shiArray[j].split("*")[1]; var shi = shistring; var shiId = shi.split("_")[0]; var shiName = shi.split("_")[1]; if (shiId == shiidparam) { var xian = xianarray.split("/"); for (var s = 0; s < xian.length; s++) { var xianId = xian[s].split("_")[0]; var xianName = xian[s].split("_")[1]; var Newoption = $("<option></option>"); $(Newoption).val(xianId); $(Newoption).text(xianName); $("#xian").append(Newoption); } } } } } </script> </head> <body> <select id="sheng" onchange="shengchange(this.value)"> </select> <select id="shi" onchange="shichange(this.value)"> </select> <select id="xian"> </select> </body> </html>
運行效果:
關于使用JavaScript怎么實現一個級聯算法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。