处理请求时显示图标的jquery ajax($.post)

处理请求时显示图标的jquery ajax($.post),jquery,Jquery,我正在使用jquery的$.post for ajax(就像用在不同的selectbox中选择的值填充selectbox)。 我为此使用了以下代码 $('#cmbState').change(function(){ $.post('<?php echo APP_URL."include/ajax.php"; ?>', {stateid: $(this).val(), option: 'getCitiesList'}, function(response){ if(respons

我正在使用jquery的$.post for ajax(就像用在不同的selectbox中选择的值填充selectbox)。 我为此使用了以下代码

$('#cmbState').change(function(){
 $.post('<?php echo APP_URL."include/ajax.php"; ?>', {stateid: $(this).val(), option: 'getCitiesList'}, function(response){
  if(response != '')
  {
   $('#cmbCity').html(response);
  }
 })
});
$('#cmbState')。更改(函数(){
$.post(“”,{stateid:$(this).val(),选项:'getCitiesList'},函数(响应){
如果(响应!='')
{
$('#cmbCity').html(回复);
}
})
});
上面的代码工作得非常好,但它没有通知用户ajax请求正在处理中,用户需要等待一段时间

我想在html控件旁边显示一个图像,直到加载响应为止

我只想要jquery的答案。 在($.ajax)上也找到了与此相关的内容,但我希望使用$.post方法实现此功能(如果可能的话)

请帮我做这个。 任何线索都将不胜感激

谢谢

将加载代码放入beforeSend函数中,该函数在调用
success
之前启动

将加载代码放入beforeSend函数中,该函数在调用
成功之前启动。

您可以在AJAX请求启动或完成时使用和全局执行一段代码,例如:

// show the '#loading' element when ajaxStart, and hide it when ajaxComplete
$("#loading").bind("ajaxStart", function(){
  $(this).show();
}).bind("ajaxComplete", function(){
  $(this).hide();
});
通过这样做,您不需要添加任何逻辑来处理所有Ajax请求上的加载消息。

您可以在Ajax请求启动或完成时使用和全局来执行一段代码,例如:

// show the '#loading' element when ajaxStart, and hide it when ajaxComplete
$("#loading").bind("ajaxStart", function(){
  $(this).show();
}).bind("ajaxComplete", function(){
  $(this).hide();
});

这样,您就不需要添加任何逻辑来处理所有Ajax请求的加载消息。

谢谢您的回答,meder先生,使用jquery的$.post方法可以实现同样的功能吗?我总是倾向于使用较低级别的
.ajax
,因为如果你必须切换方法,你可以保持其余部分不变,这样会更灵活。谢谢你的回答,meder先生,使用jquery的$.post方法可以实现相同的功能吗?我总是倾向于使用较低级别的
.ajax
,因为如果必须切换方法,可以保持其余部分不变,这样会更灵活。