一个函数首先是ajax到php后台把数据库中对应的记录删除,然后返回模板页面一个表示成功删除的值(msg==1),此时模板页面使用jquery移除对应的dom,
$('.del').live('click',function(){
    var id = $(this).next().val();
    if(id == '' || id == null){
       $(this).parent().parent().remove();  
    }else{
        confirmnew('确定要删除该商品吗?',function(){  
             $.get('__APP__/BygCom/delPF',{rand:Math.random(),fpid:id},function(msg){
                 if(msg == 1){
                     $(this).parent().parent().remove();
                 }else{
                     alert('删除失败!');
                 }
             });
         });
     }
});
红色部分是不能运行的代码,我有试着在if(msg==1)中alert('test');能够打印出test,而且数据库中的记录也是被删除了的。
删除按钮的表格结构:
<tr>
    <td>
        <input type='button' class='del' />
    </td>
</tr>

解决方案 »

  1.   


    var self = $(this);
    confirmnew('确定要删除该商品吗?',function(){   
       $.get('__APP__/BygCom/delPF',{rand:Math.random(),fpid:id},function(msg){
       if(msg == 1){
        self.parent().parent().remove();
       }else{
       alert('删除失败!');
       }
       });
       });
       }
      

  2.   

    你在回调函数里面使用this,当前this并不是$('.del')对象。不出意外的话应该是window对象。
      

  3.   

    红色的那个this是在function(msg)里面的改成:$('.del').live('click',function(){
      var that = this;
      var id = $(this).next().val();
      if(id == '' || id == null){
      $(this).parent().parent().remove();   
      }else{
      confirmnew('确定要删除该商品吗?',function(){   
      $.get('__APP__/BygCom/delPF',{rand:Math.random(),fpid:id},function(msg){
      if(msg == 1){
      $(that).parent().parent().remove();
      }else{
      alert('删除失败!');
      }
      });
      });
      }
    });
      

  4.   

    3楼的可以解决问题,可是很神奇的是,我看有的回调函数中写了$this.parent().parent().remove()能够删除,怎么换了个页面就$this就不能获取当前的dom对象了。