<script type="text/javascript"> /*分类级联下拉框动态显示 Goosoz@163.com 2013-07*/ function CategoryHelper(rootId, alllist) { function proClass(json) { this.Id = json[0]; this.ParentId = json[1]; this.Name = json[2]; } var list = new Array(); for (var i = 0; i < alllist.length; i++) { var item = new proClass(alllist[i]); list.push(item); } //获取当前proClass function getProClass(id) { for (var i = 0; i < list.length; i++) { var item = list[i]; if (item.Id == id) { return item; } } return null; }; //获取父辈proClass function getParent(id) { var curItem = getProClass(id); if (curItem == null) { return null; } for (var i = 0; i < list.length; i++) { var item = list[i]; if (item.Id == curItem.ParentId) { return item; } } return null; }; //获取下级proClass function getChildrens(id) { var arr = new Array(); for (var i = 0; i < list.length; i++) { var item = list[i]; if (item.ParentId == id) { arr.push(item); } } return arr; }; function getSiblings(id) { var curItem = getProClass(id); return getChildrens(curItem == null ? rootId : curItem.ParentId); }; function getSelect(pId, id, arr) { var html = "<select class=\"sitem\" pid=\"" + pId + "\">"; html += "<option value=\"" + pId + "\" " + (rootId == id ? "selected=\"selected\"" : "") + ">--</option>"; for (var i = 0; i < arr.length; i++) { var item = arr[i]; html += "<option value=\"" + item.Id + "\" " + (item.Id == id ? "selected=\"selected\"" : "") + ">" + item.Name + "</option>"; } html += "</select>"; return html; }; //当前选中的ID this.CurID = 0; //当前选中的名称 this.CurName = null; //当前选中的名称数组 this.CurNameArray = new Array(); //获取所有Select的Html this.getHtml = function (id) { this.CurID = id; this.CurName = null; this.CurNameArray = new Array(); if (id == rootId) { var arr = getChildrens(rootId); return getSelect(rootId, rootId, arr); } else { var _cur = getProClass(id); var _childrens = getChildrens(id); var html = ""; while (_cur != null) { this.CurNameArray.push(_cur.Name); var _siblings = getSiblings(_cur.Id); html = getSelect(_cur.ParentId, _cur.Id, _siblings) + html; _cur = getProClass(_cur.ParentId); } if (_childrens.length > 0) { html += getSelect(id, rootId, _childrens); } return html; } }; } </script>
<div id="areabox"></div>
var JS_AddressJSON=[["110000","0","北京市"],["130000","0","河南省"],["210000","0","上海市"],["130100","130000","周口市"]];
var pHelper = new CategoryHelper("0",JS_AddressJSON); var pHelper_DefaultID = JS_AreaID; $("#areabox").html(pHelper.getHtml(pHelper_DefaultID)); $(document).on("change","#areabox select", function () { $("#areabox").html(pHelper.getHtml($(this).val())); }); $("#btn-nextstep").click(function () { var area = pHelper.CurID; });