这篇文章给大家介绍使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。