zoukankan      html  css  js  c++  java
  • JS.CategoryHelper 缓存级下拉框多级联动

    <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;
        
         });

  • 相关阅读:
    wdcp升级php和mysql
    centos安装中文支持(转)
    centos5.5用phpstudy一键安装配置虚拟主机后,yum配置代理服务器squid
    http status 汇总
    关于html标签元素的data-*属性
    [分 享] PHPCMS V9 更换域名,附件地址无法批更新(更换变便)问题>解决方法!!
    svn服务器配置小记
    Camstar Portal modeling user guid --设置本地时间
    msdn webcast 下载地址整理
    mvc 项目下 webservice 程序无法运行
  • 原文地址:https://www.cnblogs.com/CodeBase/p/Javascript.html
Copyright © 2011-2022 走看看