2016-08-21 76 views
3

下面是沒有運行,我不斷得到Uncaught TypeError: $(...) is not a function(anonymous function) @ global.js:80j @ jquery.min.js:2k @ jquery.min.js:2'這是'})(jQuery);被呼叫的目標線'。我已經嘗試在文檔的<head>中重新組織我的外部腳本,並嘗試使用JS lint,唉我繼續得到這個錯誤。連續控制檯錯誤與我的jQuery自動完成功能

$(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(); 
    }); 
}); 
+2

爲什麼你將'jQuery'作爲一個參數*傳遞給已經假定存在'$'變量的函數? – nnnnnn

回答

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(); 
    }); 
}); 
+0

斑點。真的很容易看到OP的代碼的簡化版本,就像這樣:'$(function(){})()',但是我沒有注意到它的完整版本,直到你指出它。 – nnnnnn

+0

我現在得到這個錯誤與您的建議:jquery.min.js:4 Uncaught TypeError:f.getClientRects是不是一個函數 –