可编辑下拉框

$.fn.EditableSelect = function () {
    var that = this;
    this.Options = null;
    this.input = null;// 内部输入框
    this.getValue = function () {
        return this.input.val().replace(/,/gi, "");
    }
    this.init = function () {
        var theInput = $("<input/>");
        theInput.attr("id", that.attr("id") + "_input");
        var left = that.position().left + 1;
        var top = that.position().top + 1;
        var z_index = that.css("z-index") == "auto" ? 1 : that.css("z-index") + 1;
        var height = that.height() - 3;
        var width = that.width() - 18;
        theInput.css({
            position: "absolute",
            left: left,
            top: top,
            width: width,
            height: height,
            "z-index": z_index,
            "outline-style": "none",
            border: "none"
        });

        that.after(theInput);
        that.input = theInput;

    }
    this.setValue = function (value) {
        var hasTheValue = false;
        var _options = that.find("option");
        $.each(_options, function (i, o) {
            if (o.value == value) {
                hasTheValue = true;
            }
        })
        if (hasTheValue) {
            that.val(value);
        }
        that.input.val(value);
    }
    this.redraw = function () {
        var left = that.position().left + 1;
        var top = that.position().top + 1;
        that.input.css({
            left: left,
            top: top,
        });
    }
    return this.each(function () {
        that.init();
        $(window).on("resize", function () {
            that.redraw();
        })
        $(this).on("change", function () {
            if ($(this).attr("readonly")) {
                return;
            } else {
                that.input.val($(this).val());
            }
        })
        return $(this);
    })
}

使用:
var practitioners = $("#Practitioners").EditableSelect();
获取值: practitioners.getValue();
设置值:  practitioners.setValue(result.Practitioners);

 

posted @ 2016-07-06 14:44  江—城—子  阅读(221)  评论(0编辑  收藏  举报