Delete record from Modal Bootstrap window

Asked

Viewed 2,246 times

2

I’m trying to delete a record from the Bootstrap modal window, but it’s not working.

Look how I did:

By clicking the delete button of a particular record

<a class="btn btn-primary delete" href="#" data-href="#" data-target="#confirm-delete" data-toggle="modal"  id="<?php echo $codigoform;?>">Apagar experiência</a>

jQuery interprets the command:

$('a.delete').click(function() {

    var id = $(this).attr('id');
    var data = 'id=' + id ;
    var parent = $(this).parent().parent().parent().parent();

    var $meu_alerta = $("#confirm-delete");

    $meu_alerta.modal().find(".btn-ok").on("click", function() {

        $.ajax({
            type: "POST",
            url: "deletar_formacao.php",
            data: data,
            cache: false,

            success: function() {
                parent.fadeOut('slow', function() {$(this).remove();});
                window.location.reload();
            }
        });

    });
});

The "deletar_formacao.php" page reads the code through the post method and deletes the record.

I detected that it’s as if the modal window and the onclick event of the delete class were competing. Could you give me a hand?

2 answers

3

$("a.delete").click(function(){
    var $this = $(this);
    var id = $this.attr("id");
    var data = 'id=' + id ;
    var $meu_alerta = $("#confirm-delete");
    $meu_alerta.modal().find(".btn-ok").on("click", function(){
        // $.ajax[...]
    });
});

Jsfiddle

  • 3

    With var id = this.id; save one line and use less jQuery.

  • Hello, Sergio and William. Please see above my edited code. Where I’m going wrong?

  • When I run the code, the modal window opens and closes at the same time. I checked that if I hide the line -> var $meu_alerta = $("#confirm-delete") the modal appears, however, I cannot delete it.

  • I thought that alone the show was triggered by the function modal, then try with $meu_alerta.modal('show') to see if firm

  • also unsuccessful :/

  • puts in front of the $('a.delete') one unbind('click') staying that way $('a.delete').unbind('click').click( can sometimes be due to any inconsistency in the rest of the code or by the HTML structure itself that makes the button "clicked" twice and can cause this "appears" and "disappears", keep trying, that an hour works

  • also nothing :/

Show 2 more comments

1


Do it this way:

<a class="btn btn-primary delete" id="<?php echo $codigoform;?>">Apagar experiência</a>

Note that I removed the call from the modal through button.

And by jquery, you call it normally.

var id = $(this).attr('id');
            var data = 'id=' + id ;
$('#confirm-delete').modal('show'); 

        $("#confirm-delete").modal().find(".btn-ok").on("click", function(){
...

Use this logic and see if it works.

  • Now it’s worked out, guys! I thank you all!!!!

Browser other questions tagged

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