【问题标题】:ASP.NET Auto Complete DropDownListASP.NET 自动完成下拉列表
【发布时间】:2011-05-12 23:54:42
【问题描述】:

asp.net Auto Complete DropDownList 是否存在任何 Control 或 Jquery 插件? 如果是,请在此处链接示例。 我不想使用 asp.net ajax 控制工具包

【问题讨论】:

    标签: asp.net jquery autocomplete drop-down-menu


    【解决方案1】:

    我不知道这是否是您要查找的内容,但这里有一个不错的类似 Facebook 的预先输入列表:

    https://github.com/emposha/FCBKcomplete

    它是一个 jQuery 插件,使用 AJAX 调用 Web 服务以在您键入时为列表加载项目,并实现一些智能缓存以节省 Web 服务调用。就像我说的,可能不是您要找的东西,但至少值得一看。

    还可以查看 jQuery 自动完成功能。

    http://jqueryui.com/demos/autocomplete/

    【讨论】:

    • 但是我如何使用 Jquery Ui autoCOmplete 和 asp.net DropDownList?
    • 好吧,在这种情况下,您真的不想使用 asp.net 下拉列表。您想使用 jquery 自动完成功能,并使用下拉列表的可能值填充列表。代替下拉列表,您可以输入一个带有 id 并指定 runat='server' 的输入,然后在页面回发时在服务器端检查该值。
    • 这可能不是很清楚。看这里:jqueryui.com/demos/autocomplete 并确保查看源代码以查看示例代码。通过阅读该来源,您应该会更加清楚。
    • 他正在寻找的是一种适用于名称-值对的自动完成功能。下拉列表将是 ui 元素的明显选择。用户键入文本,它会过滤下拉元素的显示文本,但在提交时,它会提交所选文本的值。我正在寻找同样的东西,但没有找到即插即用的实现......
    【解决方案2】:

    jQuery AutoComplete 可以与 DropDownList 一起使用

    http://jqueryui.com/demos/autocomplete/#combobox

    【讨论】:

      【解决方案3】:
              //just put this script in your page and call the class combobox2 in your dropdownlist  
      
      
              <script type="text/javascript">
                          (function ($) {
                              $.widget("custom.combobox2", {
                                  _create: function () {
                                      this.wrapper = $("<span>")
                                      .addClass("custom-combobox2")
                                      .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 style='width:auto;'>")
                                      .appendTo(this.wrapper)
                                      .val(value)
                                      .attr("title", "")
                                      .addClass("custom-combobox2-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
                                              });
                                          },
                                          autocompletechange: "_removeIfInvalid"
                                      });
                                  },
                                  _createShowAllButton: function () {
                                      var input = this.input,
                                      wasOpen = false;
                                      $("<a>")
                                      .attr("tabIndex", -1)
                                      .attr("title", "Show All Items")
                                      .appendTo(this.wrapper)
                                      .button({
                                          icons: {
                                              primary: "ui-icon-triangle-1-s"
                                          },
                                          text: false
                                      })
                                      .removeClass("ui-corner-all")
                                      .addClass("custom-combobox2-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
                                      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 = "";
                                  },
                                  _destroy: function () {
                                      this.wrapper.remove();
                                      this.element.show();
                                  }
                              });
                          })(jQuery);
                          $(function () {
                              $(".combobox2").combobox2();
                              $(".combobox2").combobox2({
                                  select: function (event, ui) {
                                      var f = document.getElementById("<%=form1.ClientID%>");
                                          f.submit();
                                      }
                                  });
                              });
                      </script>
      
      
      <asp:DropDownList ID="DDL_Groups4_Assign" runat="server" AppendDataBoundItems="True" AutoPostBack="True" CausesValidation="True" OnSelectedIndexChanged="DDL_Groups4_Assign_SelectedIndexChanged" Width="250px" CssClass="combobox2">
                <asp:ListItem Selected="True">Select</asp:ListItem>
                                                  </asp:DropDownList>
      

      【讨论】:

        猜你喜欢
        • 2012-06-10
        • 1970-01-01
        • 1970-01-01
        • 2018-04-27
        • 2015-03-07
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多