jQuery 延迟和键盘输入
大家好,
我想做的是在有人使用 keyup 在文本字段中输入文本时显示动画 gif,一旦他们完成输入,gif 就会消失并显示消息“已保存”几秒钟,然后就会消失。
到目前为止我所做的是:
if ($('.gallery_items li input').length > 0) {
$('.gallery_items li input').keyup(function() {
var li = $(this).parent();
li.children('.gallery_saving').removeClass('dn');
var identity = li.attr('id').split('_');
var v = $(this).val();
var url = '/caption/id/' + identity[1];
$.post(url, { caption : v });
$.delay(500).li.children('.gallery_saving').delay(500).html('Saved...').delay(500).addClass('dn');
return false;
});
}
其中“gallery_ saving”类将动画gif指定为背景图像,“dn”类仅具有css“display:none”。 最初“gallery_ saving”也有类“dn” - 因此它不可见,当有人开始输入时,类“dn”被删除 - 显示加载程序。
您显然已经看到了问题:
$.delay(500).li.children('.gallery_saving').delay(500).html('Saved...').delay(500).addClass('dn');
我知道这是错误的,但不知道该怎么做 - 有人可以帮忙吗?
HI guys,
What I'm trying to do is to display the animated gif while someone is typing text into the text field with keyup and once they've finished typing, the gif to disappear and display message 'Saved' for a few seconds, which would then disappear.
What I've done so far is:
if ($('.gallery_items li input').length > 0) {
$('.gallery_items li input').keyup(function() {
var li = $(this).parent();
li.children('.gallery_saving').removeClass('dn');
var identity = li.attr('id').split('_');
var v = $(this).val();
var url = '/caption/id/' + identity[1];
$.post(url, { caption : v });
$.delay(500).li.children('.gallery_saving').delay(500).html('Saved...').delay(500).addClass('dn');
return false;
});
}
Where class 'gallery_saving' has animated gif assigned as background image and class 'dn' has simply css 'display:none'.
Initially 'gallery_saving' has class 'dn' as well - so that it isn't visible, when someone starts typing the class 'dn' is removed - showing the loader.
You obviously see the problem already with:
$.delay(500).li.children('.gallery_saving').delay(500).html('Saved...').delay(500).addClass('dn');
and I know it's wrong, but can't figure out how to do it - can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该删除加载程序,而不是延迟时间,而是在输入数据成功安全的情况下删除加载程序。
例如示例
: http://fiddle.jshell.net/gabel/udvbx/
阅读 http://api.jquery.com/jQuery.ajax/ 了解详细信息...
You should remove the loader not on delay time, but on succesful safe of the entered data..
Something like
Example: http://fiddle.jshell.net/gabel/udvbx/
Read http://api.jquery.com/jQuery.ajax/ for Details...
尝试这样做并不是一个特别好的主意,因为如果用户实际上还没有完成输入,您最终会进行大量的 ajax 调用。如果用户打字速度很慢,您将无缘无故地多次保存数据。
您还可以使用
show()
和hide()
而不是dn
类。或者使用 toggleClass()It's not a particularly good idea to try and do this, because you'll end up making a lot of ajax calls if the user hasn't actually finished typing. If the user is a slow at typing, you will be saving the data a lot of times for no reason.
You could also use
show()
andhide()
instead of thedn
class. Or use toggleClass()