Hello. I implemented the following code to delete data from the database via Ajax:

$(".delalbumimg").on("click", function(){ var res = confirm("ΠŸΠΎΠ΄Ρ‚Π²Π΅Ρ€Π΄ΠΈΡ‚Π΅ ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅"); if(!res) return false; var img = $(this).attr("alt"); //имя ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ var rel = $(this).attr("rel"); //Ρ„Π»Π°Π³ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ(0-базовая, 1-галлСрСя) var album_id = $("#album_id").text(); //ID Π°Π²Ρ‚ΠΎΡ€Π° $.ajax({ url: "./", type: "POST", data:{album_image: img, rel: rel, album_id: album_id}, success: function(res){ $(this).closest("tr").remove(); alert(this); }, error: function(){ alert("Ошибка"); } }); }); 

If successful, I need to delete the line (as a rule, without reloading the page) that contained the deleted photo. How to implement it?

    2 answers 2

    you have a different scope in success and another this. Write the target element into a variable in the click handler and access it in the handler for success and not to this.

     $(".delalbumimg").on("click", function(e){ var el=e.target; var res = confirm("ΠŸΠΎΠ΄Ρ‚Π²Π΅Ρ€Π΄ΠΈΡ‚Π΅ ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅"); if(!res) return false; var img = $(this).attr("alt"); //имя ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ var rel = $(this).attr("rel"); //Ρ„Π»Π°Π³ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ(0-базовая, 1-галлСрСя) var album_id = $("#album_id").text(); //ID Π°Π²Ρ‚ΠΎΡ€Π° $.ajax({ url: "./", type: "POST", data:{album_image: img, rel: rel, album_id: album_id}, success: function(res){ $(el).closest("tr").remove(); alert(this); }, error: function(){ alert("Ошибка"); } }); }); 

    There is also an option with bind, if you so want to use this, but all I will say about it is better not to.

      Inside the success $(this) is not the same.

      If so?

       $ (". delalbumimg"). on ("click", function () {
           var res = confirm ("Confirm removal");
           if (! res) return false;
      
           self = $ (this);
           var img = $ (this) .attr ("alt");  // name of the picture
           var rel = $ (this) .attr ("rel");  // flag of the image (0-base, 1-gallery)
           var album_id = $ ("# album_id"). text ();  // author ID
           $ .ajax ({
             url: "./",
             type: "POST",
             data: {album_image: img, rel: rel, album_id: album_id},
             success: function (res) {
               self.closest ("tr"). remove ();
               alert (this);
             },
             error: function () {
               alert ("Error");
             }
           });
       });