0

ドロップダウンリストにhttp://jqueryui.com/autocomplete/#comboboxを使用していますが、うまく機能します。ここでの問題は、コード ビハインドからドロップダウン リストを無効にすると機能しないことです。

私のコードはここにあります。

ASPX

<asp:DropDownList runat="server" CssClass="Dropdown" ID="ddlStatus"> </asp:DropDownList>

JS

    function pageLoad() {
 /******** Auto Complete *********************/
                $(function () {
                    $("#<%= ddlStatus.ClientID %>").combobox();
                });

                (function ($) {
                    $.widget("custom.combobox", {
                        _create: function () {
                            this.wrapper = $("<span>")
                              .addClass("custom-combobox")
                              .insertAfter(this.element);

                            this.element.hide();
                            this._createAutocomplete();
                            this._createShowAllButton();
                        },

                        _createAutocomplete: function () {
                            var selected = this.element.children(":selected"),
                              value = selected.val() ? selected.text() : "";

                            this.input = $("<input>")
                              .appendTo(this.wrapper)
                              .val(value)
                              .attr("title", "")
                              .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
                              .autocomplete({
                                  delay: 0,
                                  minLength: 0,
                                  source: $.proxy(this, "_source")
                              })
                              .tooltip({
                                  tooltipClass: "ui-state-highlight"
                              });

                            this._on(this.input, {
                                autocompleteselect: function (event, ui) {
                                    ui.item.option.selected = true;
                                    this._trigger("select", event, {
                                        item: ui.item.option
                                    });
                                    __doPostBack('<%= upnlTab.ClientID %>', this.element.attr("id"));
                                },
                                autocompletechange: "_removeIfInvalid"
                            });
                        },

                        _createShowAllButton: function () {
                            var input = this.input,
                              wasOpen = false;

                            $("<a>")
                              .attr("tabIndex", -1)
                              //.attr("title", "Show All Items")
                              //.tooltip()
                              .appendTo(this.wrapper)
                              .button({
                                  icons: {
                                      primary: "ui-icon-triangle-1-s"
                                  },
                                  text: false
                              })
                              .removeClass("ui-corner-all")
                              .addClass("custom-combobox-toggle ui-corner-right")
                              .mousedown(function () {
                                  wasOpen = input.autocomplete("widget").is(":visible");
                              })
                              .click(function () {
                                  input.focus();

                                  // Close if already visible
                                  if (wasOpen) {
                                      return;
                                  }

                                  // Pass empty string as value to search for, displaying all results
                                  input.autocomplete("search", "");
                              });
                        },

                        _source: function (request, response) {
                            var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                            response(this.element.children("option").map(function () {
                                var text = $(this).text();
                                if (this.value && (!request.term || matcher.test(text)))
                                    return {
                                        label: text,
                                        value: text,
                                        option: this
                                    };
                            }));
                        },

                        _removeIfInvalid: function (event, ui) {

                            // Selected an item, nothing to do
                            if (ui.item) {
                                return;
                            }

                            // Search for a match (case-insensitive)
                            var value = this.input.val(),
                              valueLowerCase = value.toLowerCase(),
                              valid = false;
                            this.element.children("option").each(function () {
                                if ($(this).text().toLowerCase() === valueLowerCase) {
                                    this.selected = valid = true;
                                    return false;
                                }
                            });

                            // Found a match, nothing to do
                            if (valid) {
                                return;
                            }

                            // Remove invalid value
                            if (value != '') {
                                this.input
                                  .val("")
                                  .attr("title", value + " didn't match any item")
                                  .tooltip("open");
                                this.element.val("");
                                this._delay(function () {
                                    this.input.tooltip("close").attr("title", "");
                                }, 2500);
                                this.input.data("ui-autocomplete").term = "";
                            } else {
                                this.input.val("");
                                this.element.val("");
                                this.input.data("ui-autocomplete").term = "";
                            }
                            __doPostBack('<%= upnlTab.ClientID %>', this.element.attr("id"));
                        },

                        _destroy: function () {
                            this.wrapper.remove();
                            this.element.show();
                        }
                    });
                })(jQuery);


            }

C#

私がこれを呼んでいるいくつかの検証に基づいて、

 ddlStatus.Enabled = false;

ここに答えがありますが、自分のコードで動作させることはできません。 基になる DropDownList が無効になっている場合は jQuery ComboBox を無効にします

4

1 に答える 1

0

これが私がやった方法です。* * で囲まれた変更を参照してください。

(function ($) {
$.widget("custom.combobox", {
    _create: function () {
        this.wrapper = $("<span>")
          .addClass("custom-combobox")
          .insertAfter(this.element);

        this.element.hide();
        *select = this.element.hide();*                           
        this._createAutocomplete(*select*);
        this._createShowAllButton(*select*);
    },

    _createAutocomplete: function (*select*) {
        var selected = this.element.children(":selected"),
            select = this.element.hide(),
          value = selected.val() ? selected.text() : "";
        *var disabled = select.is(':disabled');*
        this.input = $("<input>")
          .appendTo(this.wrapper)
          .val(value)
          .attr("title", "")
          .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
            .*attr('disabled', disabled)*
          .autocomplete({
              delay: 0,
              minLength: 0,
              source: $.proxy(this, "_source")
          })
          .tooltip({
              tooltipClass: "ui-state-highlight"
          });

        this._on(this.input, {
            autocompleteselect: function (event, ui) {
                ui.item.option.selected = true;
                this._trigger("select", event, {
                    item: ui.item.option
                });
                __doPostBack('<%= upnlTab.ClientID %>', this.element.attr("id"));
            },
            autocompletechange: "_removeIfInvalid"
        });
    },

    _createShowAllButton: function (*select*) {
        var input = this.input,
          wasOpen = false;
        *var disabled = select.is(':disabled');*
        console.log(this.element.attr("id") + " : " + disabled);
        $("<a>")
          .attr("tabIndex", -1)
          *.attr('disabled', disabled)*
          //.attr("title", "Show All Items")
          //.tooltip()
          .appendTo(this.wrapper)
          .button({
              icons: {
                  primary: "ui-icon-triangle-1-s"
              },
              text: false
          })
          .removeClass("ui-corner-all")
          .addClass("custom-combobox-toggle ui-corner-right")
          .mousedown(function () {
              wasOpen = input.autocomplete("widget").is(":visible");
          })
          .click(function () {
              *if ($(this).attr('disabled')) {
                  return false;
              }*
              input.focus();

              // Close if already visible
              if (wasOpen) {
                  return;
              }

              // Pass empty string as value to search for, displaying all results
              input.autocomplete("search", "");
          });
    },

    _source: function (request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        response(this.element.children("option").map(function () {
            var text = $(this).text();
            if (this.value && (!request.term || matcher.test(text)))
                return {
                    label: text,
                    value: text,
                    option: this
                };
        }));
    },

    _removeIfInvalid: function (event, ui) {

        // Selected an item, nothing to do
        if (ui.item) {
            return;
        }

        // Search for a match (case-insensitive)
        var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
        this.element.children("option").each(function () {
            if ($(this).text().toLowerCase() === valueLowerCase) {
                this.selected = valid = true;
                return false;
            }
        });

        // Found a match, nothing to do
        if (valid) {
            return;
        }

        // Remove invalid value
        if (value != '') {
            this.input
              .val("")
              .attr("title", value + " didn't match any item")
              .tooltip("open");
            this.element.val("");
            this._delay(function () {
                this.input.tooltip("close").attr("title", "");
            }, 2500);
            this.input.data("ui-autocomplete").term = "";
        } else {
            this.input.val("");
            this.element.val("");
            this.input.data("ui-autocomplete").term = "";
        }
        __doPostBack('<%= upnlTab.ClientID %>', this.element.attr("id"));
    },

    _destroy: function () {
        this.wrapper.remove();
        this.element.show();
    }
});

})(jQuery);

于 2013-09-15T09:25:45.657 に答える