Uncaught TypeError: this.source is not a function when using rails4-autocomplete

Posted

技术标签:

【中文标题】Uncaught TypeError: this.source is not a function when using rails4-autocomplete【英文标题】: 【发布时间】:2016-04-02 01:10:15 【问题描述】:

我目前正在尝试在搜索字段上设置自动完成功能。我正在使用 rails4-autocomplete gem。我在这里阅读了文档:https://github.com/peterwillcn/rails4-autocomplete 并按照所有步骤使其工作,并且确实有效。我开始在搜索框中输入内容,得到了预期的结果。

我的下一步是实现一键搜索功能。当我单击其中一个自动完成建议时,我希望站点转到该项目的相应页面。我在此页面上使用了名为 Autocomplete Magic with Rails 的建议,地址为 http://www.yoniweisbrod.com/autocomplete-magic-with-rails/

基本上,您添加一个事件挂钩,在选择项目时提交搜索按钮。

$( document ).ready(function() 
  $('.search-query').bind('railsAutocomplete.select', function(event, data 
    $('.search-me').trigger('click')
  );
);

当我实现它时,这种方法会起作用,有时当我点击我希望它进入页面的项目时,有时它不会去任何地方。它只是用项目信息填充文本框,但不提交。在这些情况下,我收到一个 jQuery 错误,内容如下:

 Uncaught TypeError: this.source is not a function - autocomplete.self-3befc48aba87cead090cfd014562b2771a662ac6d2c8197b24c08b2d9f3d9f2d.js?body=1:440 

刷新页面解决了这个问题,这让我认为我的 javascript 没有按正确的顺序加载。所以我实现了 jQuery Turbolinks gem 以确保我的 jQuery 事件总是在没有页面刷新的情况下加载。这没有帮助。

为了提供信息,我的 jQuery 如下所示:

$( "#username" ).autocomplete( html: true );

将 html 选项设置为 true 允许我在搜索结果中呈现 html。

我一直在谷歌搜索,但到目前为止还没有运气。任何想法,任何人?谢谢!

【问题讨论】:

【参考方案1】:

我相信你没有将来源添加到autocomplete

var data = ['one', 'two'];
$("#your-id").autocomplete(
  data
);

【讨论】:

感谢您的建议。不幸的是,指定自动完成的来源似乎没有什么区别。 试试这个:`$("#your-id").autocomplete(source: data); 这正是我所做的,但没有运气: $( "#username" ).autocomplete( html: true , source: source_array);

以上是关于Uncaught TypeError: this.source is not a function when using rails4-autocomplete的主要内容,如果未能解决你的问题,请参考以下文章

App.jsx:43 Uncaught (in promise) TypeError: this.state.concerts.map is not a function(...)

Uncaught TypeError: this.canvas.getContext is not a function

Uncaught TypeError: Cannot set properties of undefined (setting ‘XXX‘) 报错

Uncaught (in promise) TypeError: this.setState is not a function

Uncaught (in promise) TypeError: this.setState is not a function

AboutHeader.jsx:21 Uncaught TypeError: this.setState is not a function