JS级联下拉框

时间:2023-03-09 01:50:11
JS级联下拉框

//Ajax级联获取SDK
function GetDropDownList(parent_ddlID, fill_dllID, url, param) {
    this.pId = parent_ddlID;  //主下拉框name
    this.cId = fill_dllID;    //级联下拉框name
    this.URL = url;           //获取数据路径
    this.Paramer = param;     //参数
}
GetDropDownList.prototype.Inits = function (current) {
    var $ddl = $("select[name$=" + this.pId.toString() + "]");
    var $ddlSDKs = $("select[name$=" + this.cId.toString() + "]");
    $ddl.focus();
    $ddl.bind("change keyup", function () {
        if ($(this).val() != "0") {
            current.loadSDKList(current, $("select option:selected").val());
            $ddlSDKs.fadeIn("slow");
        } else {
            //$ddlSDKs.fadeOut("slow");
            $ddlSDKs.children("option").remove();
            $ddlSDKs.append($("<option></option>").val("0").html("不限"));
        }
    });
}
GetDropDownList.prototype.loadSDKList = function (current, selectedItem) {
    $.ajax({
        type: "get",
        url: this.URL,
        data: this.Paramer + "=" + selectedItem,
        dataType: "json",
        async: true,
        success: function (ret) {
            current.printSDKList(ret);
        }
    });
}
GetDropDownList.prototype.printSDKList = function (data) {
    $("select[name$=" + this.cId.toString() + "] > option").remove();
    for (var i = 0; i < data.length; i++) {
        $("select[name$=" + this.cId.toString() + "]").append(
                        $("<option></option>").val(data[i].Value).html(data[i].Text)
                   );
    }
}

//调用

//var d = new GetDropDownList('主下拉框name', '级联下拉框name', '获取数据路径', '传递给调用路径的参数');
//d.Inits(d);

GetDropDownLis