简体   繁体   English

未捕获的TypeError:$(…).autocomplete不是函数

[英]Uncaught TypeError: $(…).autocomplete is not a function

I'm using the JQuery UI autocomplete for quite a few text fields on my Backbone.js project. 我在Backbone.js项目中的许多文本字段中使用了JQuery UI自动完成功能。 The libraries are loaded in the correct order as follows, 这些库按以下正确顺序加载:

define(['jquery','jquery-ui.min','jquery.ui.touch-punch']);

The autocomplete function gets called, like so: 自动完成功能被调用,如下所示:

$("#channelRspm").autocomplete({
 minLength: 3,
 delay: 1000,
 source: function(request, response) {
 var results = $.ui.autocomplete.filter(channel, request.term);
 response(results.slice(0, 10));
   }
});

The problem is that the autocomplete behavior is very random. 问题在于自动完成行为非常随机。 When running the source on Chrome browser, sometimes it works flawlessly. 在Chrome浏览器上运行源代码时,有时它可以完美运行。 However, sometimes I get the error in console : 但是,有时我在console中收到错误:
Uncaught TypeError: $(...).autocomplete is not a function 未捕获的TypeError:$(...)。autocomplete不是函数

Refreshing the html normally makes it work again but then, that is not what I want. 刷新html通常可以使其再次工作,但是那不是我想要的。 Could anyone please suggest where I'm going worng? 有人可以建议我要去哪里吗?

You should make sure is loaded after using shim . 您应该确保使用shim之后加载了

require.config({
  paths: {
    "jquery": "lib/jquery",
    "jquery-ui": "lib/jquery-ui",
    "underscore": "lib/underscore",
    "backbone": "lib/backbone"
  },
  shim: {
    "underscore": {
        exports: "_"
    },
    "backbone": {
        exports: "Backbone",
        deps: ["underscore", "jquery"]
    },
    "jquery-ui": {
        exports: "$",
        deps: ['jquery']
    }
  }
});

You need to specify jquery-ui as dependency for jquery.ui.touch-punch in similar way. 您需要以类似方式将jquery-ui指定为jquery.ui.touch-punch依赖jquery.ui.touch-punch

then define your module like 然后像这样定义你的模块

define(['jquery','jquery-ui','jquery.ui.touch-punch']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM