Script re-execution problem in jQuery after the same event is run

Asked

Viewed 23 times

0

I am developing a web system in ASP.NET MVC and I have some registered companies, I created a button on this grid that leaves the company active and inactive in the system and I am creating a jQuery script to delete the companies and display them again.

My problem is that when I click on the company button it deletes all content from div #grid-elementos excluding all companies, does a search of the list of companies and displays to the user. Everything normally occurs but after the event the use of the Active or Inactive button does not re-enter the script again.

I’ve tried so hard to use the $("#grid-elementos").empty() how to use the $("#grid-elementos").remove() as I tried to do the same actions in div $("#elemento") or how $("#grid-elementos elemento") but the same problems occur.

   <div id="grid-elementos">
           @foreach (var item in Model)
           {
               <div id="elemento" class="Search">
                   <div id="campos-elemento">
                       <a href="/Empresa/EditEmpresa/@item.id_pessoa">
                           <div id="desc-elemento">
                               @Html.DisplayFor(modelItem => item.nome)
                           </div>
                           <div id="cod-elemento">
                               Cod.: @Html.DisplayFor(modelItem => item.id_pessoa)
                           </div>
                           <div id="cnpjcpf-elemento">
                               CNPJ.: @Html.DisplayFor(modelItem => item.cpf_cnpj)
                           </div>
                       </a>
                   </div>
                   <div id="botoes-elemento">
                       <ul>                      
                           <li><button type="button" class="botao-elemento-ativo btn-status" data-item="@item.id_pessoa">@Html.DisplayFor(modelItem => item.situacao)</button></li>
                           <li><button type="button" class="botao-elemento-delete btn-delete" data-item="@item.id_pessoa">Deletar</button></li>
                       </ul>
                   </div>
               </div>
            }
       </div>  

jquery code

       $(".btn-status").click(function () {
           var id = $(this).attr('data-item');
           alert(id);
           if (confirm("Tem certeza que deseja alterar o status do registro?")) {
               $.ajax({
                   method: "POST",
                   url: "/Empresa/AlteraStatus/" + id,
                   success: function (data) {
                       $("#grid-elementos").empty();
                       $.each(data, function (i, item) {
                           $("#grid-elementos").append(
                               "<div id=\"elemento\" class=\"Search\" >" +
                               "<div id=\"campos-elemento\" >" +
                               "<a href=\"/Empresa/EditEmpresa/" + item.id_pessoa + "\" >" +
                               "<div id=\"desc-elemento\" >" + item.nome +
                               "</div>" +
                               "<div id=\"cod-elemento\" >"
                               + "Cod.: " + item.id_pessoa +
                               "</div>" +
                               "<div id=\"cnpjcpf-elemento\" >"
                               + "CNPJ.: " + item.cpf_cnpj +
                               "</div>" +
                               "</a>" +
                               "</div>" +
                               "<div id=\"botoes-elemento\" >" +
                               "<ul>" +
                               "<li><button type=\"button\" class=\"botao-elemento-ativo btn-status\" data-item=\"" + item.id_pessoa + "\" >" + item.situacao + "</button></li>" +
                               "<li><button type=\"button\" class=\"botao-elemento-delete btn-delete\" data-item=\"" + item.id_pessoa + "\" >Deletar</button></li>" +
                               "</ul>" +
                               "</div>" +
                               "</div>"
                           );
                       });
                   },
                   error: function (data) {
                       alert("Houve um erro na pesquisa.");
                   }
               });
           }
       });

I’m taking a risk in this project on my own but know very little javascript programming like jQuery or Ajax.

  • @Ricardopontual was just that and it was solved.

  • this problem is an old acquaintance :)

No answers

Browser other questions tagged

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