原因来源于jAutoComboBox2插件的使用中 效果总是有偏差,所以编写了此脚本
重写了大部分逻辑
Version 0.9.0
Date: 2011-8-22 
Original author zlddian
Modified By   ayun00
Email: [email protected]允许自由复制,自由使用,请不要修改以上信息
如果有修改请mail我一份
也欢迎各位提出修改意见数据格式 var data = [{ "id": 25, "text": "淮安分公司", "children": [{ "id": 26, "text": "办公室(行政人事培训)", "children": [{ "id": 28, "text": "办公室(行政人事培训333)", "children": [{ "id": 30, "text": "办公室(行政人事培训333)"}]}] }, { "id": 29, "text": "办公室(行政人事培训2)"}] }, { "id": 52, "text": "研发部", "children": [{ "id": 54, "text": "研发部2222"}] }, { "id": 53, "text": "财务部"}];id: 填充select的 value
text: 填充select 的 text
children: 下一级的数据主函数
ComboBoxGetValue 得到指定 层级的 selec 值或者text , 层级 留空为取最后一级select
ComboBoxGetAllPath 得到从第一个select 到最后一个select 的  selec 值或者text 字符串 
调用方式
$("#div_UnlimitedSelect").UnlimitedSelect({
                cssClass: "autoComboBox",
                nullDispaly: true,
                data: data,
                firstValue: [true, "==请选择==", "==请选择=="],
                defaultValue: 29
            });
        });
        function getval() {
            var a = $("#div_autoComboBox").ComboBoxGetValue(null, "value", ErrorFn);
            alert(a);
        }
        function getalltext() {
            var a = $("#div_autoComboBox").ComboBoxGetAllPath("text", "-");
            alert(a);
        }        function ErrorFn() {
            alert("错误了");
        }

解决方案 »

  1.   


    /*
    编写此脚本的原因来源于jAutoComboBox2插件的使用
    //重写了大部分逻辑
    Version 0.9.0
    Date: 2011-8-22 
    Original author zlddian
    Modified By   ayun00
    Email: [email protected]允许自由复制,自由使用,请不要修改以上信息
    如果有修改请mail我一份
    也欢迎各位提出修改意见数据格式 var data = [{ "id": 25, "text": "淮安分公司", "children": [{ "id": 26, "text": "办公室(行政人事培训)", "children": [{ "id": 28, "text": "办公室(行政人事培训333)", "children": [{ "id": 30, "text": "办公室(行政人事培训333)"}]}] }, { "id": 29, "text": "办公室(行政人事培训2)"}] }, { "id": 52, "text": "研发部", "children": [{ "id": 54, "text": "研发部2222"}] }, { "id": 53, "text": "财务部"}];id: 填充select的 value
    text: 填充select 的 text
    children: 下一级的数据主函数
    ComboBoxGetValue 得到指定 层级的 selec 值或者text , 层级 留空为取最后一级select
    ComboBoxGetAllPath 得到从第一个select 到最后一个select 的  selec 值或者text 字符串 
    调用方式
    $("#div_UnlimitedSelect").UnlimitedSelect({
                    cssClass: "autoComboBox",
                    nullDispaly: true,
                    data: data,
                    firstValue: [true, "==请选择==", "==请选择=="],
                    defaultValue: 29
                });
            });
            function getval() {
                var a = $("#div_autoComboBox").ComboBoxGetValue(null, "value", ErrorFn);
                alert(a);
            }
            function getalltext() {
                var a = $("#div_autoComboBox").ComboBoxGetAllPath("text", "-");
                alert(a);
            }        function ErrorFn() {
                alert("错误了");
            }
    */(function() {    $.extend($.fn, {
            //开始        //初始化
            UnlimitedSelect: function(op) {
                op = $.extend({
                    url: false,                         //获取数据的地址
                    remote: false,  //是否进行远程请求     
                    type: "POST",                         //请求远程数据的方式 get/post
                    quest: "q",  //请求的参数名
                    data: true,                         //数据(Json格式)  只有当url参数设置为false时该参数才生效 否则优先从远程url获取数据
                    isWebservice: false,                 //数据是否来源于web服务 (下个版本实现)
                    cssClass: false,                    //下拉框使用的class样式
                    firstValue: [true, "0", "请选择"],  //是否自动创建第一个值  这个值通常为 请选择 ,不限等
                    idPrefix: "Unlimited_Select_ID",       //自动生成下拉框的 默认ID前缀
                    defaultValue: false,                 //如果此值不为false  将根据该值设置默认值 并且递归向上设置所有的下拉框默认值                nullDispaly: true,    //是否显示所有子层级(第一个下拉项值的子层级) //下个版本实现                onchange: null,     //改变下拉框时候触发的事件
                    onchangeresult: null         // 定值为returntype 的 text, value  ,如果不为空返回当前类型
                }, op);            var c = this;
                if (op.remote) {    //从远程获取数据
                    c.remoteajax(c, op, 1, "", op.defaultValue);
                }
                else {
                    if (op.defaultValue) {  //得到默认值的层级,如果有默认值
                        c.path = c.getpath(op.data, [], op.defaultValue, 0);
                        if (!c.issearc) {
                            c.path = [];
                        }
                    }
                    c.CreateOption(c, 1, op, op.data, true);
                }        },
     
      

  2.   


           // o 当前UnlimitedSelect对象
            // index 当前层级
            // op  参数
            // data 当前层级的数据
            // 是否第一次填充
            CreateOption: function(o, index, op, data, isfirst) {
                if (data.length > 0) {
                    var $select;
                    if (o.find("#" + op.idPrefix + index).length > 0) {      //查询当前递归的下拉框是否存在 如果存在则删除
                        $select = o.find("#" + op.idPrefix + index);
                        $select.get(0).options.length = 0;
                    }
                    else {
                        $select = $('<select id="' + op.idPrefix + index + '" index="' + index + '"/>');   //创建新的下拉框
                        o.append($select);
                    }
                    //附加用户自定义下拉框样式
                    if (op.cssClass) {
                        $select.addClass(op.cssClass);
                    }                var ComboBox = this;
                    var tempOption = '';
                    if (op.firstValue[0]) {
                        tempOption += "<option value='" + op.firstValue[1] + "'>" + op.firstValue[2] + "</option>"
                    }
                    $.each(data, function(i, item) {
                        if (o.path[index - 1] == item.id && isfirst) {
                            tempOption += "<option value='" + item.id + "' selected='selected' >" + item.text + "</option>"
                        }
                        else {
                            tempOption += "<option value='" + item.id + "'>" + item.text + "</option>"
                        }
                    });
                    $select.empty().append(tempOption);
                    //绑定下拉框 onchange事件开始
                    $select.change(function() {
                        //绑定下拉框 onchange事件
                        if ($.isFunction(op.onchange)) {
                            //根据onchangeresult 向 自定义事件传递 全路径
                            var rb;
                            if (op.onchangeresult) {
                                rb = o.ComboBoxGetAllPath(op.onchangeresult);
                            }
                            op.onchange(rb);
                        }
                        //
                        nextIndex = index + 1;
                        //创建后面的下拉框的时候 必须清除所有后面的下拉框
                        var j = nextIndex;
                        while (true) {
                            //寻找当前下拉框的 
                            $select = o.find("#" + op.idPrefix + j);
                            if ($select.length > 0)
                                $select.remove();
                            else {
                                break;
                            }
                            j++;
                        }
                        //回调递归
                        var val = $(this).val();
                        if (op.remote) {
                            ComboBox.remoteajax(o, op, nextIndex, val, op.defaultValue);
                        }
                        else {
                         
                            //遍历当前项,如果有子项,进行创建
                            $.each(data, function(i, item) {
                                if (item.id == val) {
                                    if (item.children) {
                                        ComboBox.CreateOption(o, nextIndex, op, item.children);   //change时 回调该方法
                                        add = true;
                                        return false;
                                    }
                                }
                            });
                        }                });
                    //绑定下拉框 onchange事件结束
                    //如果有默认值,并且再下一级中 那么创建下一个下拉框                
                    if (ComboBox.path.length >= index && isfirst) {
                        ComboBox.CreateOption_NotFirst(o, nextIndex, ComboBox.path[index - 1], op, data);
                        //ComboBox.CreateOption(o, index+1, op, data.children,false); 
                    }
                }
            },       
      

  3.   

     CreateOption_NotFirst: function(o, index, val, op, data) {
                var ComboBox = this;
                $.each(data, function(i, item) {
                    if (item.id == val) {
                        if (item.children) {
                            ComboBox.CreateOption(o, index, op, item.children, true);   //找到默认值 回调方法
                            return false;
                        }
                    }
                    /*  if (item.children) {
                    ComboBox.CreateOption_NotFirst(o, index, val, op, item.children);
                    }*/
                });
            },        /*
            请求当前最后一个select的全路径
            value_text_rec value 还是 text
            splitstr 连接符
            errFn 错误回调函数
            */
            ComboBoxGetAllPath: function(value_text_rec, splitstr, errFn) {
                var s = this.find("select");
                if (value_text_rec) {            }
                else {
                    value_text_rec = "value";
                }
                if (splitstr)
                { }
                else {
                    splitstr = "";
                }
                try {
                    switch (value_text_rec) {
                        case "value":
                            var rtn;
                            for (var i = 0; i < s.length; i++) {
                                if (!rtn) {
                                    rtn = s.eq(i).val();
                                }
                                else {
                                    rtn += splitstr + s.eq(i).val();
                                }
                            }
                            return rtn
                        case "text":
                            var rtn;
                            for (var i = 0; i < s.length; i++) {
                                if (!rtn) {
                                    rtn = s.eq(i).find('option:selected').text();
                                }
                                else {
                                    rtn += splitstr + s.eq(i).find('option:selected').text();
                                }
                            }
                            return rtn
                    }
                }
                catch (err) {
                    if (errFn) {
                        errFn(err);
                    }
                    return "";
                }
            },
            /*
            返回第几级的值  默认返回最后一个下拉框的值  不过不满足条件弹出自定义错误消息
            */
            ComboBoxGetValue: function(Atleast, value_text_rec, errFn) {
                var s = this.find("select");
                if ((Atleast) || (Atleast == 0)) {
                    if (s.length - 1 >= Atleast) {
                        if (value_text_rec) {
                            switch (value_text_rec) {
                                case "value":
                                    return s.eq(Atleast).val();
                                case "text":
                                    return s.eq(Atleast).find('option:selected').text();
                                case "rec":
                                    return s.eq(Atleast);
                            }
                        }
                        else {
                            return s.eq(Atleast).val();
                        }
                    }
                    else {
                        if (errFn) {
                            errFn();
                        }
                        return "";
                    }
                }
                else {
                    if (value_text_rec) {
                        switch (value_text_rec) {
                            case "value":
                                return s.eq(s.length - 1).val();
                            case "text":
                                return s.eq(s.length - 1).find('option:selected').text();
                            case "rec":
                                return s.eq(s.length - 1);
                        }
                    }
                    else {
                        return s.eq(s.length - 1).val();
                    }
                }
            },        /*
            c 当前对象
            op 参数
            index 当前层级
            rid 请求的值
            defaultValue 默认值
            */
            remoteajax: function(c, op, index, rid, defaultValue) {
                $.ajax({
                    type: op.type,
                    url: op.url,
                    data: "defaultValue=" + defaultValue + "&" + op.quest + "=" + rid,
                    success: function(msg) {
                        op.data = eval(msg);
                        //重新得到 默认层级
                        if (op.defaultValue) {  //得到默认值的层级,如果有默认值
                            c.path = c.getpath(op.data, [], op.defaultValue, 0);
                        }
                        if (!c.issearc) {
                            c.path = [];
                        }
                        c.CreateOption(c, index, op, op.data);                }, error: function() {
                        alert("not connection server");
                    }
                });
            },        issearc: false,
            //data  遍历数据
            //org 返回的数组
            //寻找的id
            //层次
            getpath: function(data, org, id, levle) {            var ddd = this;
                if (typeof levle == "undefined") {
                    levle = 0;
                }
                var curlevele = levle;
                $.each(data, function(i, v) {
                    if (!ddd.issearc) {
                        //console.log("查找项:" + id + "当前值" + v.id + ",当前深度:" + curlevele + ",当前arr:" + org.join(','));
                        if (v.id == id) {
                            org[curlevele] = id;
                            ddd.issearc = true;
                            //清除大于当前路径的值
                            //console.log("长度" + org.length + ",当前深度:" + curlevele);
                            if (curlevele < org.length) {
                                org.splice(curlevele + 1, org.length - curlevele);
                            }
                            return false;
                        }
                        //递归
                        else if (v.children) {
                            org[curlevele] = v.id;
                            org = ddd.getpath(v.children, org, id, curlevele + 1);
                        }
                    }
                    else {
                        return false;
                    }
                });
                return org;
            },
            //当前层级
            path: [],
            returntype: ["value", "text"]        //结束
        });
    })(jQuery);
      

  4.   

    谢谢分享用jquery快两年了,还没写过插件
      

  5.   

    ComboBox.CreateOption_NotFirst(o, nextIndex, ComboBox.path[index - 1], op, data);
    这一行有个小错误 修改为ComboBox.CreateOption_NotFirst(o, index +1, ComboBox.path[index - 1], op, data);