我正在使用jquery的$ .post来表示ajax(比如在不同的selectbox中选择一个值来填充选择框)。 我使用了以下代码
$('#cmbState').change(function(){
$.post('<?php echo APP_URL."include/ajax.php"; ?>', {stateid: $(this).val(), option: 'getCitiesList'}, function(response){
if(response != '')
{
$('#cmbCity').html(response);
}
})
});
上面的代码完全正常,但它不会通知用户ajax请求正在进行中,他必须等待一段时间。
我想在html控件旁边显示一个图像,直到加载响应为止。
我只想用jquery来回答这个问题。 在jquery docs上找到了与此相关的内容($ .ajax),但我想使用$ .post方法实现此功能(如果可能的话)。
请帮我解决这个问题。 非常感谢任何线索
感谢
答案 0 :(得分:14)
您可以使用ajaxStart和ajaxComplete全局Ajax Events在AJAX请求开始或完成时执行一段代码,例如:
// show the '#loading' element when ajaxStart, and hide it when ajaxComplete
$("#loading").bind("ajaxStart", function(){
$(this).show();
}).bind("ajaxComplete", function(){
$(this).hide();
});
通过这样做,您不需要添加任何逻辑来处理您所做的所有Ajax请求上的加载消息。
答案 1 :(得分:7)
$.ajax({
url:'/',
data: params,
type:'POST',
beforeSend: function() {
alert("loading")
},
success:function(html){ alert(html) }
});
将加载代码放在beforeSend函数中,该函数在调用success
之前触发。