我的jQuery自动完成功能连续出现控制台错误

时间:2016-08-21 01:30:55

标签: javascript jquery jquery-ui

以下内容未运行,我不断收到Uncaught TypeError: $(...) is not a function(anonymous function) @ global.js:80j @ jquery.min.js:2k @ jquery.min.js:2&#39;这是以&#39;为目标的。 })(jQuery);打来电话。我尝试在文档的<head>中重新组织我的外部脚本,并尝试使用JS lint,唉,我继续得到这个错误。

$(document).ready(function() {
  $(function($, undefined) {
    $.widget("app.autocomplete", $.ui.autocomplete, {
      _create: function() {
        if(this.element.is("select")) {
          var self = this;
          this.original = this.element.hide();
          this.element = $("<input/>").insertAfter(this.original);
          this.options.source = function(request, response) {
            var filter = $.ui.autocomplete.filter,
              $options = self.original.find("option"),
              result = $options.map(function() {
                return $(this).val();
              });
            response(filter(result, request.term));
          };
        }
        this._super("_create");
      },
      _destroy: function() {
        this._super("_destroy");
        this.element.remove();
        this.original.show();
      }
    });
  })(jQuery);
  $(function() {
    $("#autocomplete").autocomplete();
  });
});

1 个答案:

答案 0 :(得分:1)

您不需要将jQuery变量传递给新创建的函数,因为$已经具有对jQuery函数的引用。您收到错误,因为当函数作为第一个参数传递时,函数$不会返回函数。

$(document).ready(function() {
  $.widget("app.autocomplete", $.ui.autocomplete, {
    _create: function() {
      if(this.element.is("select")) {
        var self = this;
        this.original = this.element.hide();
        this.element = $("<input/>").insertAfter(this.original);
        this.options.source = function(request, response) {
          var filter = $.ui.autocomplete.filter,
            $options = self.original.find("option"),
            result = $options.map(function() {
              return $(this).val();
            });
          response(filter(result, request.term));
        };
      }
      this._super("_create");
    },
    _destroy: function() {
      this._super("_destroy");
      this.element.remove();
      this.original.show();
    }
  });
  $(function() {
    $("#autocomplete").autocomplete();
  });
});