-1

I want to create a new function that when I press a little trashcan icon, a record in MYSQL will be deleted.

The following is my js code:



        $("table").find("img[class='icon_garbage']").click(function(){

            var delnum = $(this).parents("tr").find("td[class='so']").text();
            var string='delete_num='+delnum;

            $.ajax({
                url:"../src/delete.php",
                type:"post",
                data:string,
                success: function(){
                  var tr = $(this).parents("tr");
                  tr.fadeOut('slow', function(){
                     $(this).remove();
                   });
                }

        })
    });

and it's not working.

the record do delete, but the record does't fadeout.

but if I fix the code as following:



        $("table").find("img[class='icon_garbage']").click(function(){

            var delnum = $(this).parents("tr").find("td[class='so']").text();
            var string='delete_num='+delnum;
            var tr = $(this).parents("tr");
                   tr.fadeOut('slow', function(){
                      $(this).remove();
                   });


            $.ajax({
                url:"../src/delete.php",
                type:"post",
                data:string,
                success: function(){
     //              var tr = $(this).parents("tr");
     //              tr.fadeOut('slow', function(){
     //                 $(this).remove();
    //               });
                }

        })
    });

and it's will working ......

I don't know why , can somebody tell me ?

My OS is MAC OSX 10.8.5,and I use MacVim to be my editor.

StBlade
  • 287
  • 6
  • 18
petercilee
  • 73
  • 1
  • 3
  • 11
  • 1
    see http://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working – Rituraj ratan Oct 14 '13 at 08:24
  • What isn't working? is the click function running? is the table row being deleted correctly? is the "string" variable containing the right data (never name a variable string by the way), is anything being called on delete.php? – Chris Morris Oct 14 '13 at 08:26

1 Answers1

0

Set the context in the ajax call to this. Without setting the context, this does not refer to your element in the success event.

$.ajax({
    url:"../src/delete.php",
    type:"post",
    data:string,
    context: this, // <----------------- here
    success: function(){
      var tr = $(this).parents("tr");
      tr.fadeOut('slow', function(){
         $(this).remove();
       });
    }
});
MrCode
  • 63,975
  • 10
  • 90
  • 112