我有一个向mvc控制器提交表单的函数,如下所示-
function submitForm() {
$.ajax
({
type: 'POST',
url: '/Users/Index',
data: $('#searchForm').serialize(),
beforeSend: function() {
$('.usersearchresult').fadeOut('fast', function () { $('.loadinggif').show(); });
},
success: function (response) {
$('.loadinggif').hide();
$('.usersearchresult').hide().html(response).fadeIn('normal');
}
});
return false;
}
这很好,除非响应返回得太快,$('.loadinggif').hide();
在 $('.usersearchresult').hide().html(response).fadeIn('normal');
之后发生$('.usersearchresult').hide().html(response).fadeIn('normal');
。
我尝试了不同的回调函数组合($('.loadinggif').hide();
调用$('.usersearchresult').hide().html(response).fadeIn('normal');
on callback,甚至反过来调用,而且行为总是相同的)。
我对jquery并不熟悉;任何帮助都将不胜感激!
发布于 2013-06-19 14:17:50
问题是,如果响应返回得太快,fadeOut
动画还没有完成。
要解决这个问题,请使用stop
方法,它将结束动画:
success: function (response) {
$('.usersearchresult').stop();
$('.loadinggif').hide();
$('.usersearchresult').hide().html(response).fadeIn('normal');
}
请注意,stop
将阻止调用回调,因此.loadinggif
将永远不会显示响应是否在fadeOut
动画仍在运行时返回。同时调用$('.usersearchresult').stop();
和$('.loadinggif').hide();
是最简单的。它们是相互排斥的状态,因此您可以测试loadinggif是否显示并确定是否应该调用stop
或hide
。
https://stackoverflow.com/questions/17193543
复制相似问题