如何在不重新加载的情况下刷新页面上的div

时间:2011-04-09 13:39:59

标签: ajax partial-page-refresh

在我的表中,我有来自数据库的结果,当我点击表中的链接删除它时,我试图让表刷新而不重新加载页面。在下面的代码中它工作,但我仍然需要刷新整个页面才能够点击另一个链接。 STUCK !!

$(document).ready(function(){

        $('td a').click(function(){
            //alert($(this).attr('id'));
            var id =$(this).attr('id');
            var img_name =$(this).attr('name');
            //alert(img_name);

            $.ajax({
            type: "POST",
            url: "remove.php",
            cache: false,
            data: "id="+ id +"&img_name="+ img_name,
            success: function(message){
              alert(message);
           }
        });

        var url = "delete.inc.php"; //create random number

          setTimeout(function() {
         $("#table").load(url+" #table>*");
           }, 1000); //wait one second to run function
        /////////////////////// 
        });

1 个答案:

答案 0 :(得分:0)

问题是你的click事件被绑定到'td a',一旦重新加载表,它就会被删除并替换掉。结果它只会工作一次。每次重新加载表格时都需要设置点击事件。

我建议为你的ajax请求创建一个单独的函数。然后在$(文档).ready ...

中设置click事件
$(document).ready(function(){
    $('td a').click( function_name );
});

并将其设置在您的成功函数中......

...
success: function(message){
    alert(message);
    $('td a').click( function_name );
}
...
相关问题