JS Duplicating When Sending Record

1

I have an application with Modal bootstrap, but when sending the data with the button it duplicates the event, it follows the js with the duplication.

$(document).on('click', '.form-delete', function(e){
    e.preventDefault();
    var $form=$(this);
    $('#title_delete').val($(this).data('title'));
    $('#confirm').modal({ backdrop: 'static', keyboard: false })
        .on('click', '#delete-btn', function(){
            alert($form);
        });
});

The operation is as follows, if you close the modal and open again, clicking the delete button and sending the data sends the data of the previous and current modal.

Follow the test link link

    
asked by anonymous 24.07.2018 / 15:45

1 answer

1

Separate events and declare a global variable to popular form:

var $form;

$(document).on('click', '.form-delete', function(e){
    e.preventDefault();
    $form = $(this);
    $('#title_delete').val($(this).data('title'));
    $('#confirm').modal({ backdrop: 'static', keyboard: false });
});

$(document).on('click', '#delete-btn', function(){
    alert($form);
});

What happens is that in your code each time you click .form-delete you attach a new click event to # delete-btn. So it is generating a progressive increase of alerts.

Your updated fiddle: link

    
24.07.2018 / 15:50