0

我使用这个 jquery 代码向服务器发送请求并从服务器获取响应。在这里,我正在使用此响应数据更新一个表。这个过程完美无缺。

我的问题是我需要根据脚本的行为显示一些消息。这意味着我需要在表格更新时显示类似于“正在加载,请稍等几秒钟”的消息。更新表格后,我需要显示类似“万岁,您已成功更新用户表格”的消息。

谁能告诉我如何用 jquery 完成这个?

这是我到目前为止的代码:

$.ajax({
    type: "POST",
    url: "process.php", 
    dataType: 'html',
    data: { 
        name: $('#name').val(), 
        address: $('#address').val(), 
        city: $('#city').val() 
    },
    success:function(data){
        $('#manage_user table > tbody:last').find('tr:first').before(data);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(thrownError);
    }, 
    complete: function(){
        //alert('update success'); 
    }
});

谢谢你。

4

2 回答 2

1

你可以这样做:

$.ajax({
    type: "POST",
    url: "process.php", 
    dataType: 'html',
    data: { 
        name: $('#name').val(), 
        address: $('#address').val(), 
        city: $('#city').val() 
    },
    beforeSend: function(){$('#loading').show();},
    success:function(data){
        $('#manage_user table > tbody:last').find('tr:first').before(data);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(thrownError);
    }, 
    complete: function(){
        $('#loading').hide(); 
    }
});

HTML:

<img id="loading" src="loading.gif"/>

CSS:

#loading{display:none}
于 2013-06-28T09:04:44.573 回答
0

您可以附加此处理程序以在触发 ajax 事件时启动。

$(document).ajaxStart(function() {
         $( ".message" ).text( "加载表格" );
    });
并使用完成的处理程序允许您显示完成的消息

<pre>$(document).ajaxComplete(function() {
   $( ".message" ).text( "Table Loaded" );
});</pre>
于 2013-06-28T09:09:30.153 回答