jQuery ajax($ .post)在处理请求时显示一个图标 [英] jquery ajax ($.post) showing an icon while the request is being processed

查看:219
本文介绍了jQuery ajax($ .post)在处理请求时显示一个图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将jquery的$ .post用于ajax(例如使用在不同选择框中选择的值填充选择框). 我为此使用了以下代码

I am using jquery's $.post for ajax (like populating a selectbox with a value selected in different selectbox). I used the following code for this

$('#cmbState').change(function(){
 $.post('<?php echo APP_URL."include/ajax.php"; ?>', {stateid: $(this).val(), option: 'getCitiesList'}, function(response){
  if(response != '')
  {
   $('#cmbCity').html(response);
  }
 })
});

上面的代码可以很好地工作,但是它不会通知用户ajax请求正在处理中,他必须稍等片刻.

The code above works perfectly fine but it does not notify the user that the ajax request is in process and he has to wait a bit.

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

I want to show an image next to the html control which is displayed until the response is loaded.

我只想要jquery的答案. 在 jquery文档($ .ajax)上也找到了与此相关的东西,但我想使用以下方法实现此功能$ .post方法(如果可能).

I want the answer in terms of jquery only. Found something related to this on jquery docs also ($.ajax) but I want to achieve this functionality using $.post method (if it is possible).

请帮助我. 任何线索都将不胜感激

Please help me with this. any clues are greatly appreciated

谢谢

推荐答案

您可以使用 ajaxStart ajaxComplete 全局 Ajax事件,以便在AJAX请求开始或完成时执行一段代码,例如:

You can use the ajaxStart and ajaxComplete global Ajax Events to execute a piece of code whenever an AJAX request starts or completes, for example:

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

这样做,您不需要为您提出的所有Ajax请求添加任何逻辑来处理加载消息.

By doing so, you don't need to add any logic for handling the loading message on all the Ajax requests you make.

这篇关于jQuery ajax($ .post)在处理请求时显示一个图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆