无法确定何时隐藏和显示使用 JQuery 的加载动画
我有一个加载动画,最初隐藏在我的 application.js 文件中:
$('#loading_field').hide();
我有一个自动完成字段,我希望动画在用户开始输入时出现,并在自动完成建议结果出现时消失。下面是我的 jquery ui 自动完成插件的 jquery 代码:
$(".showable_field").autocomplete({
minLength: 1,
source: '/followers.json',
focus: function(event, ui) {
$('.showable_field').val(ui.item.user.name);
return false;
},
select: function(event, ui) {
var video_id = $('#video_id_field').val();
var user_id = ui.item.user.id;
$.post("/showable_videos.js", {video: video_id, user: user_id});
$(':input','#new_showable_video').not(':button, :submit, :reset, :hidden').val('');
return false;
}
});
var obj = $(".showable_field").data('autocomplete');
obj && (obj._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.user.name + "</a>" )
.appendTo( ul );
});
我应该在哪里显示和隐藏动画?
I have a loading animation which I initially hide in my application.js file:
$('#loading_field').hide();
I have an autocomplete field, and I want the animation to appear when the user starts typing, and for it to disappear when the autocomplete suggestion results appear. Below is my jquery code for the jquery ui autocomplete plugin:
$(".showable_field").autocomplete({
minLength: 1,
source: '/followers.json',
focus: function(event, ui) {
$('.showable_field').val(ui.item.user.name);
return false;
},
select: function(event, ui) {
var video_id = $('#video_id_field').val();
var user_id = ui.item.user.id;
$.post("/showable_videos.js", {video: video_id, user: user_id});
$(':input','#new_showable_video').not(':button, :submit, :reset, :hidden').val('');
return false;
}
});
var obj = $(".showable_field").data('autocomplete');
obj && (obj._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.user.name + "</a>" )
.appendTo( ul );
});
Where should I show and hide the animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,jQuery UI 在加载远程数据时会自动将“ui-autocomplete-loading”类添加到您的输入中,因此最简单的方法就是简单地将动画 GIF 作为输入中的背景图像类存在。
这就是他们在 jQueryUI.com 上的 远程加载示例 中所做的事情(请注意,您必须在本例中,再输入两个字符来启动 ajax 调用)。
Well, jQuery UI automatically adds the class 'ui-autocomplete-loading' to your input when it is loading remote data, so the absolute easiest way to do this is to simply put an animated GIF in as the background image on your input when that class is present.
That's what they do in the remote-loaded example on jQueryUI.com (note that you have to type two more more characters to kick off the ajax call in this example).
因为您使用 AJAX 来加载建议,所以我认为这应该适合您:
Because you're using AJAX to load the suggestions I think this should work for you: