jQuery自动完成功能不适用于Turbolinks [英] jQuery autocomplete not working with Turbolinks
问题描述
我正在使用Rails4.
I'm using Rails 4. This snippet from my shared.js.coffee
works fine without turbolinks:
jQuery ->
$('input.autocomplete,textarea.autocomplete').each (index, item) =>
sel = $(item)
sel.autocomplete
source: sel.data('autocomplete-source')
minLength: 1
但是当我使用 Turbolinks 时,它仅在页面刷新时有效.在网站上浏览后,它不起作用.
But when I use Turbolinks, it only works on a page refresh. It does not work after navigating through the site.
I've read Rails 4, Turbolinks, and Delegated Event Handling in jQuery, so I have some idea of what is going wrong, but I'm not yet sure how to fix the problem.
还有一个相关的问题:如果 jQuery UI自动完成插件-或就此而言,一般来说,插件-是否旨在与Turbolinks配合使用?
And, a related question: can anyone comment definitely if the jQuery UI autocomplete plugin -- or for that matter, plugins in general -- is/are designed to work with Turbolinks?
推荐答案
使用 jQuery Turbolinks .
如果您的大型代码库中有许多
$(el).bind(...)
Turbolink,您会感到惊讶.大部分JavaScript都将以通常的方式停止工作.这是因为绑定事件的节点已不存在."
"if you have a large codebase with lots of
$(el).bind(...)
Turbolinks will surprise you. Most part of your JavaScripts will stop working in usual way. It's because the nodes on which you bind events no longer exist."
用法:
# Gemfile:
gem 'jquery-turbolinks'
# JavaScript manifest file:
//= require jquery.turbolinks
也许在某个时候,jQuery $.widget
函数将提供一个不同的绑定选项,但是在此之前,它仍然有效.无需对Javascript进行其他任何更改.
Perhaps, at some point, the jQuery $.widget
function will offer an option to bind differently, but until then, this works. No other changes to the Javascript are required.
这篇关于jQuery自动完成功能不适用于Turbolinks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!