多个 jQuery 函数 &自动完成
我发现使用声明 jQuery Autocomplete 作为脚本中的第一个函数会阻止执行其他函数。
下面的第二个和第三个函数将不起作用:
<script>
$(function() {
$("#ent_input").autocomplete('get_suggestions',
{autoFill: true, scroll: false});
});
// Freebase Suggest
$(function() {
$("#ent_input_fb").suggest({type:'/film/director'});
});
$(function() {
$("#ent_input_fb2").suggest({type:'/film/actor'});
});
</script>
但是,如果我将自动完成移动到底部,则其他脚本可以工作,但自动完成不能。
<script>
// Freebase Suggest
$(function() {
$("#ent_input_fb").suggest({type:'/film/director'});
});
$(function() {
$("#ent_input_fb2").suggest({type:'/film/actor'});
});
$(function() {
$("#ent_input").autocomplete('get_suggestions',
{autoFill: true, scroll: false});
});
</script>
有没有人遇到过 jQuery Autocomplete 类似的问题?
I found that using declaring jQuery Autocomplete as the first function in your script prevents other functions from being executed.
The second and third functions below will not work:
<script>
$(function() {
$("#ent_input").autocomplete('get_suggestions',
{autoFill: true, scroll: false});
});
// Freebase Suggest
$(function() {
$("#ent_input_fb").suggest({type:'/film/director'});
});
$(function() {
$("#ent_input_fb2").suggest({type:'/film/actor'});
});
</script>
However, if I move autocomplete to the bottom, the other script works, but autocomplete doesn't.
<script>
// Freebase Suggest
$(function() {
$("#ent_input_fb").suggest({type:'/film/director'});
});
$(function() {
$("#ent_input_fb2").suggest({type:'/film/actor'});
});
$(function() {
$("#ent_input").autocomplete('get_suggestions',
{autoFill: true, scroll: false});
});
</script>
Has anyone run into any similar issues with jQuery Autocomplete?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 应该按照声明的顺序执行所有
$(function()...)
处理程序,没有问题。看来您对autocomplete
和suggest
的调用是罪魁祸首。jQuery 对事件处理程序非常谨慎,如果您尝试使用单击处理程序执行与上述类似的操作,您会注意到它们也按顺序执行(而不是相互覆盖)。尝试一下:
假设 someElement 标记如下:
...和 JavaScript
您将看到按以下顺序显示的三个警报框:“Me First!”、“1”和“2”。
jQuery 付出了额外的努力,将标记的 onclick 处理程序保留为事件发生时要单击的第一个函数。这使得将该库与服务器端技术(例如 ASP.NET)一起使用非常安全,ASP.NET 到处都有 onclick 处理程序。
要从元素中删除所有 jQuery 分配的单击事件处理程序,请执行以下操作:
请记住,这不会删除以其他方式分配的单击处理程序(即标记中的 onclick 属性)。
jQuery should execute all of your
$(function()...)
handlers in the order they have been declared without issue. It looks like your calls toautocomplete
andsuggest
are the culprits.jQuery is extremely cautious about event handlers, and if you tried to do something similar to the above with click handlers you'll notice that they too execute in sequence (rather than overwrite each other). Try it:
Assume someElement is marked up as follows:
...and the JavaScript
You will see three alert boxes shown in the following order: "Me First!," "1," and "2."
jQuery goes the extra mile to retain your marked up onclick handlers as the first function to click when the event occurs. This makes it very safe to use this library with server-side technology like ASP.NET, which sprinkles onclick handlers all over the place.
To remove all jQuery-assigned click event handlers from an element, do the following:
Bear in mind that this does not remove a click handler assigned some other way (i.e., the onclick attribute in markup).