现在遇到的问题我用下面的图说明图一图二
上图中的父级型号中是级联的下拉框,给定品牌型号回填到下拉框中就出现如上问题(图一所示)没有激活2级下拉框,但是手工选择的话可以弹出2级下拉框
现贴一下代码:
code1:回填数据viewentity.prototype.getBrand = function () {
            var guid = UrlParm.parm("id");
            $.ajax({
                contentType: 'text/json',
                url: PageUrl.local + "WebServices/CommonService.svc/GetBrandModels_byBrandModelID",
                type: 'post',
                data: '{"guid":"' + guid+ '"}',
                async: false,
                success: function (result) {
                    if (result != null) {
                        debugger;
                        result = result.d;
                        for (var i = 0; i < result.length; i++) {
                            $("#ddl_Brand").val(result[i].value.split(";")[0]);
                            $("#txt_er").val(result[i].value.split(";")[1]);
                            if (result[i].key.split(";")[2].indexOf("@") < 0) {
                                setTimeout("$(\"#ddl_Parenter option[value!='']\").remove()",600);
                            } else {
                                var parentIDs = result[i].key.split(";")[2].split("@");
                                //35720130703473956176
                                setTimeout("$(\"#ddl_Parenter option[value='" + parentIDs[parentIDs.length - 2] + "']\").attr('selected', true)", 600);                               
                                $("#CasCategory1 select").each(function (n) {
                                    for (var j = parentIDs.length-1; j >= 0; j--) {
                                        $(this).val(parentIDs[j - 1]);
                                        break;
                                    }
                                    if ($("#CasCategory1 select").length - 1 == n) {
                                        $(this).remove();
                                    }
                                });
                            }
                        }
                    }
                }
            });
        };code2:页面html  <th>父级型号</th>
    <td class="fordata">
         <div class="Unlimited_Cascade">
            <span class="Control_Parent" id="CasCategory">
                <select id="ddl_Parenter" class="text-input" data-bind="foreach: models">
                    <option data-bind="text: catename, attr: { value: cateid }"></option>
                </select>
                <input type="hidden" id="hfCategory" />
            </span>
            <span id="CasCategory1"></span>
        </div>code3:初始化    function loadjs() {
        LoadScript(myloaddata.js_PageUrl + "Javascript/Jquery.Plugins/jquery-1.4.1.js", function () {
            $.Expand.cachedScript(PageUrl.js + "Javascript/Jquery.Plugins/knockout.js").done(function () {
                jQuery.Expand.Script(PageUrl.js + "Javascript/User.Customize/Unlimited.Cascade.js", function () {
                    myloaddata.myview = new viewentity();
                    ko.applyBindings(myloaddata.myview);
                    myloaddata.um = eval('(' + myloaddata.result.um + ')');
                });
            });
            myapply._islogin = -9;
        });    } 
viewentity.prototype.load = function () {
            self.models = ko.observableArray([]);
            self.iscurrent = ko.computed(function () { return "current" }, self);
            viewentity.prototype.initSearchArea();
            if (UrlParm.parm("id") != undefined) {
                setTimeout("viewentity.prototype.getBrand()", 300);
                self.loadtitle = ko.observable('修 改');
            } else {
                self.loadtitle = ko.observable('添 加');
            }
            var obj = {
                functionRedirect: [PageUrl.local + 'WebServices/CommonService.svc/GetBrandModel', PageUrl.local + 'WebServices/CommonService.svc/GetBrandModel_Parent'],
                Cs: 'ddl_Parenter',
                HfID: 'hfCategory',
                ResultID: 'CasCategory1'
            };
            self.c3 = new Cascade(obj);
            self.c3.init();
        }限于字数限制,cascade.js跟在2楼

解决方案 »

  1.   

    code4:Unlimited.Cascade.js
    Cascade = function (obj) {
        var C = this;
        C.CateHash = new Hash();
        C.ParentHash = new Hash();
        C.OpFunction = PageUrl.common + 'getcategorysubs';
        C.OpFunctionParent = PageUrl.common + 'getcatesub_parent';
        C.ParentID = "";
        C.subindex = 0;
        C.ParentCallback = obj.ParentCallback;
        C.EmptyCallback = obj.EmptyCallback;
        C.SelectID = obj.Cs;
        C.HfID = obj.HfID;
        C.ResultID = obj.ResultID;
        C.Where = 0;
        C.Level = null;
        C.selectitem = [];
        if (obj.Level != undefined) {
            C.Level = obj.Level;
        }
        C.DefaultOption = true;
        if (obj.DefaultOption != undefined) {
            C.DefaultOption = obj.DefaultOption;
        }
        if (obj.functionRedirect != undefined) {
            C.OpFunction = obj.functionRedirect[0];
            C.OpFunctionParent = obj.functionRedirect[1];
        }    /*回调方法;*/
        C.Callback = jQuery.extend({
            callback: function () { return false; }
        }, C.Callback || {});
        C.isGuid = function (gid) {
            var reEmail = /^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$/;
            return reEmail.test(gid);
        }
        C.init = function () {
           $("#" + C.SelectID).bind("change", function () {
                var changeID = $(this).val();
                var wh = $(this).attr("val");
                C.Where = wh == undefined ? 0 : wh;
                C.LoasProvinces(changeID);
            });
        }
        //绑定控件改变时
        C.LoasProvinces = function (changeID) {
            C.subindex = 0;
            $("#" + C.ResultID).empty();
            var seitems = $("select", $("#" + C.ResultID)).length;
            {
                C.ParentID = changeID;
                var _result = C.CateHash.getItem(changeID);
                if (_result == undefined) {
                    C.Callback.callback = C.CreateCagegory;
                    C.GetCategory(changeID);
                }
                else {
                    C.CreateCagegory(_result);
                }
                if (C.ParentCallback != undefined && C.Level == seitems) {
                    C.ParentCallback.callback(changeID);
                }
                if (C.multiSelect != undefined && C.multiSelect.Level == seitems.length) {
                    C.multiSelect.SelectCallback.callback(Parentresult[j].value);
                }
            }
        }
        ///获取级联数据  
        C.GetCategory = function (SubID) {
            if (obj.functionRedirect != undefined) {
                $.ajax({
                   ....                    C.Callback.callback(result);
                    }
                });
            } else {
            var _data = "ParentID=" + SubID + "&status=Enable";
           ajax...    }
        }
        
        //创建级联数据
        C.CreateCagegory = function (result) {
            var seitems = $("select", $("#" + C.ResultID)).length;
            if (C.Level != null) {
                if (seitems >= C.Level && C.Level != 0) {
                    return;
                }
            }
            if (result != null && result.length > 0) {
                var select = $("<select></select>");
                $(select).attr("id", result[0].key.split(";")[1]);
                $("#" + C.ResultID).append($(select));
                $(select).bind("change", function () {
                    var _parent = $(this).parent();
                    C.BindSelectChange(this);
                    if (C.ParentCallback != undefined && (C.Level == $("select", $("#" + C.ResultID)).length || C.Level == 0)) {                    C.ParentCallback.callback($(this).val());
                    }
                });
                if (C.DefaultOption == true) {
                    var _option = $("<option value=''>    </option>");
                    $(select).append($(_option));
                }
                var Parentresult = C.ParentHash.getItem(C.ParentID);
                var _boo = 0;
                for (var i = 0; i < result.length; i++) {
                    if (C.Where == 1 && result[i].key.split(";")[2] != "1") {
                        /* 过滤数据*/
                        continue;
                    }
                    var option = $("<option value='" + result[i].key.split(";")[0] + "' val='" + result[i].key.split(";")[2] + "'>" + result[i].value + "</option>");
                    if (Parentresult != undefined && _boo == 0) {
                        for (var j = 0; j < Parentresult.length; j++) {
                            if (result[i].key.split(";")[0] == Parentresult[j].value) {
                                $(option).attr("selected", true);
                                _boo = 1;
                                break;
                            }
                        }
                    }
                    $(select).append($(option));
                    if (_boo == 1) {
                        C.BindSelectChange($(select));
                        _boo = 2;
                    }
                }            if (C.ParentCallback != undefined && (C.Level == $("select", $("#" + C.ResultID)).length || C.Level == 0)) {
                    C.ParentCallback.callback($(select).val());
                }
            } else {
                if (C.ParentCallback != undefined) {
                    C.ParentCallback.callback();
                }
            }
            C.Where = null;
        }
        C.BindSelectChange = function (obj) {        var parent = $(obj).parent();
            C.subindex = $(obj, $(parent)).index();
            var ID = $(obj).val();
            var _result = null;
            {
                var _result = C.CateHash.getItem(ID);
                if (_result == undefined) {
                    C.Callback.callback = C.BindSub;
                    C.GetCategory(ID, obj);
                }        }
            C.BindSub(_result, obj);
        }
          C.getselectitem = function () {
            var selectlist = $("select", $(".Unlimited_Cascade"));
            for (var i = 0; i < selectlist.length; i++) {
                var cid = $(selectlist[i]).val();
                var cname = $(selectlist[i]).find("option:selected").text();
                if ($.trim(cid) != "") {
                    C.selectitem.push({ id: cid, name: cname });
                }
            }
        };
        C.getselectitemname = function () {
            var cnames = "";
            var selectlist = $("select", $(".Unlimited_Cascade"));
            for (var i = 0; i < selectlist.length; i++) {
                var cid = $(selectlist[i]).val();
                if ($.trim(cid) != "") {
                    cnames += $(selectlist[i]).find("option:selected").text()+";";
                }
            }
        };
        C.getselectitemid = function () {
            var cids = "";
            var selectlist = $("select", $(".Unlimited_Cascade"));
            for (var i = 0; i < selectlist.length; i++) {
                var cid = $(selectlist[i]).val();
                if ($.trim(cid) != "") {
                    cids += cid + ";";
                }
            }
        };
    }
      

  2.   

    代码赋值。不会触发onchange\
    自己手动调用一下 对应函数
      

  3.   

    document.getElementById('ddl_Parenter').fireEvent('onchange');
    这样貌似也不可行
      

  4.   

    C.getParent=function(){
    ...
    $.ajax({
    success:function(a){
    var parslt = $("#selectid");
    var paropts = $("option",$(parslt));
    for(var i=0;i<paropts.length;i++){
    if($(paropts[i]).val()==a){
    $(paropts[i]).attr("selected",true);
    //层数多就多循环几次
    }
    }
    }
    })
    ...
    }