0

Please help me with refreshing the page after post success, instead of animate. I've tried window.opener.location.href, but it doesn't do any good. Thanks for your help!

$(function () {
    $(".delbutton").click(function () {
        //Save the link in a variable called element
        var element = $(this);
        //Find the id of the link that was clicked
        var del_id = element.attr("id");
        //Built a url to send
        var info = 'id=' + del_id;
        if (confirm("Are you sure you want to remove this item?")) {
            $.ajax({
                type: "GET",
                url: "delete",
                data: info,
                success: function () {

                }
            });
            $(this).parents(".record").animate({
                backgroundColor: "#fbc7c7"
            }, "fast")
                .animate({
                opacity: "hide"
            }, "slow"); //instead of this, I want page to refresh after confirmation
        }
        return false;
    });
});
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71

3 Answers3

0

Why not just delete the element corresponding with the id on success?

Edited to add animation while removing the element:

  $(function () {
    $(".delbutton").click(function () {
        //Save the link in a variable called element
        var element = $(this);
        //Find the id of the link that was clicked
        var del_id = element.attr("id");
        //Built a url to send
        var info = 'id=' + del_id;
        if (confirm("Are you sure you want to remove this item?")) {
            $.ajax({
                type: "GET",
                url: "delete",
                data: info,
                success: function () {
                   // ADDITION HERE
                $('div[id='+ del_id +']').hide('slow', function(){ $('div[id='+ del_id +']').remove(); });

                }
            });

        }
        return false;
    });
});
Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • Hi Mrk, I just tried that--the query went through, but page did not animate. I swear the x-editable plug-in doesn't play well with this script block... – user2675057 Dec 12 '13 at 03:18
  • Ok didn't you say you didn't want to animate it, you just wanted to remove the deleted link from the page? – Mrk Fldig Dec 12 '13 at 11:47
  • Ok so its deleting the the element but not animating? – Mrk Fldig Dec 12 '13 at 19:36
  • Just a shot in the dark, change $(".delbutton").click(function () { TO $(".delbutton").click(function (e) { and insert the first line of that click function as e.stopPropagation(); – Mrk Fldig Dec 12 '13 at 19:51
  • Thats really rather odd, is the click function deleting it and not animating it? I do think its maybe a problem relating to x-editable, do you have this project online so I can take a look? – Mrk Fldig Dec 13 '13 at 10:43
  • Mrk, no project online--I'm still testing it at the local level. At this point, I don't really care if it animates, I just want it to refresh! – user2675057 Dec 13 '13 at 15:56
0

I'm not sure why you are refreshing the page after an AJAX call, but if you really need to, this is how to do it. When the user confirms removal, send an AJAX request, and if successful, refresh the page.

if (confirm("Are you sure you want to remove this item?")) {
    $.ajax({
        type: "GET",
        url: "delete",
        data: info,
        success: function () {
            window.location.reload();
        }
    });
}
TheCarver
  • 19,391
  • 25
  • 99
  • 149
  • @user2675057 try `window.location.href = window.location.href` instead of `window.location.reload()`. – TheCarver Dec 12 '13 at 03:04
  • I've tried both window.location.href = window.location.href and window.location.reload(), the page reloads, but the data did not go through the database. Any ideas? – user2675057 Dec 12 '13 at 03:15
0

Turns out that it was not a problem with the any third party plug-ins but I needed to include a <tr class="record">. It works after that!