我有以下jQuery Ajax调用:
$.ajax({
type: "POST",
url: "addtobasket.php",
data: "sid=<?= $sid ?>&itemid=" + itemid + "&boxsize=" + boxsize + "&ext=" + extraval,
success: function(msg){
$.post("preorderbasket.php", { sid: "<?= $sid ?>", type: "pre" },
function(data){
$('.preorder').empty();
$('.preorder').append(data);
});
}
});
我想在ajax调用进行时显示图像。我怎么能这样做?
谢谢,
答案 0 :(得分:27)
试试这个:
<script type="text/javascript">
$(document).ready(function()
{
$('#loading')
.hide()
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
});
});
</script>
<div id="loading">
Loading....
</div>
这将显示每次执行ajax请求时的加载图像,我已经在页面顶部实现了这个div,所以它不会阻碍页面,但是你总能看到ajax调用何时开始上。
答案 1 :(得分:5)
这些内容(showLoadingImage
和hideLoadingImage
取决于您):
// show the loading image before calling ajax.
showLoadingImage();
$.ajax({
// url, type, etc. go here
success: function() {
// handle success. this only fires if the call was successful.
},
error: function() {
// handle error. this only fires if the call was unsuccessful.
},
complete: function() {
// no matter the result, complete will fire, so it's a good place
// to do the non-conditional stuff, like hiding a loading image.
hideLoadingImage();
}
});
答案 2 :(得分:1)
您可以在调用$ .ajax()之前显示图像,然后在后处理函数中隐藏/删除图像(就在您的.empty()/。append(数据)调用之前。