我有通过AJAX显示数据的页面,直到获取结果时,我希望显示一个加载程序,并且在获取结果后,我希望加载程序消失。下面是我试图显示加载程序的代码的一部分
var onSubmit = function(e)
{
var txtbox = $('#txt').val();
var hiddenTxt = $('#hidden').val();
$("#walkaway").hide();
$("#submitamt").hide();
$("#xtrabutton").hide();
LodingAnimate();
$.ajax(
{
type: 'post',
url: 'test2.php',
dataType: 'json',
data: {txt: txtbox,hidden: hiddenTxt},
cache: false,
success: function(returndata)
{
$('#first').html(returndata[0]);
$('#second').html(returndata[0]);
$('#third').html(returndata[0]);
},
error: function()
{
console.error('Failed to process ajax !');
}
});
function LodingAnimate()
{
$("#maillist").html('<img src="img/ajax-loader.gif" /> Please Wait Loading...');
}
}; 在单击按钮时,执行上述AJAX,在单击加载程序之后,但即使在我获得回复(在控制台中)之后,加载程序仍继续运行,但实际上它应该已经消失了。
发布于 2015-09-09 07:04:28
在ajax调用完成后,您应该手动隐藏/移除加载器,添加Jquery Complete回调并添加代码以删除加载程序。
Jquery -这个回调将在执行成功和错误回调后执行。
$.ajax(
{
type: 'post',
url: 'test2.php',
dataType: 'json',
data: {txt: txtbox,hidden: hiddenTxt},
cache: false,
success: function(returndata)
{
//Success code
},
error: function()
{
//error code
},
complete:function()
{
//This block will execute after success/error executes.
//Make the loader div empty
$("#maillist").empty();
}
});https://stackoverflow.com/questions/32472843
复制相似问题