Update menu via ajax

Asked

Viewed 242 times

1

When I clicked on the menu item of my application it always updated the page, I am changing to update via ajax the pages however I am having difficulties in relation to functions with names based on the class event, example the delete function when I click is called the function of the previous page and the current page. Could you remove everything from the previous page? Example: I click on the city page and after clicking on the Parents page, the Open function loads the index, when I click on the delete button is called 2 times, and the city index is no longer being demonstrated.

function Open(url, descricao) {
       // event.preventDefault();
        url = '@Url.Content("~/")' + url;
        $.ajax({
            url: url,
            type: 'GET',
            success: function (response) {
                console.log("s");
                $('#principal').html($(response).find('#Conteudo2'));
                window.history.pushState("object or string", descricao, url);

            },
            error: function () {
                alert('Ocorreu um erro!');
            }
        });
    }
    
    $(document).on("click", ".btn-delete", function () {
            var link = $(this).attr("id");
            var removeItemEl = $(this);

            confirm("Você tem certeza que deseja excluir o pais registro?",

                function () {
                    $.ajax({
                        type: "POST",
                        url: "Pais/delete",
                        data: {
                            id: link
                        },
                        success: function (data) {
                            removeItemEl.closest("tr").remove();
                        },
                        error: function (data) {
                            alert("O País já foi utilizado, não é possível deletar o registro!");
                        }
                    })
                }

                , "Confirmação de exclusão");


        });

  • It calls the event from the previous page, example clicked on the screen and city and then on the screen of Parents routine calls the event from the 2 screens. @dvd

  • No I load a screen first and in onclic I call the open method that loads the second screen, after if I interact with the delete button is called 2 times the @dvd function

  • @dvd the screen are the index.

  • @dvd yes each a different index and different functions.

  • @dvd that’s right

  • @dvd worked like this, however I have more events with this problem, so I need to do this for everyone? Is there any way to undo all? Or it’s better to create events with unique names for each index?

  • Close thanks @dvd

Show 2 more comments

1 answer

0


Since you are loading external pages that come with the same events that already exist, you can cancel current events before loading the new content using the method .off() jQuery.

You can cancel an event for a specific selector:

$(document).off("click", ".btn-delete");

Or you can cancel them all:

$(document).off();

More about the method .off() in official documentation.

  • a question when using the command off it blocks the menu opening event,use the data-toggle inside the <a> tag to link with the <ul> tag. You know what can be?

  • @Zica I updated the answer.

  • Thanks worked, a question you consider it good practice to make update like this? Because you have to remember to add the classes to remove, which can be a problem. I followed this post to make this idea https://answall.com/questions/31954/comort-fazer-isso-usando-renderbody-do-asp-net-mvc

  • 1

    I think every page you upload should have its own functions. You can even use the same class. btn-delete, but there should be another selector that differentiates one from the other. For example, a div that "houses" the button with another class or id so it wouldn’t have this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.