Duplicate JS when sending record

Asked

Viewed 91 times

1

I have an application with bootstrap modal, but when sending the data with the button it duplicates the event, follow the js with 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 close the modal and open again, when clicking the delete button and send the data it sends the data of the previous and the current modal.

Follow the test link https://jsfiddle.net/308s2ftk/3/

  • Can you describe how the upload is done better? You may need html to view the Forms.

  • I’ll make it work and post

  • I put the link of the test, note that if you close the modal and open again, click delete it will duplicate the event

  • Try to bind like this: $(document).unbind('click').on('click' ...

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 every time you click . form-delete you attach a new click event to #delete-btn. That’s why it’s generating a progressive increase in Alerts.

Your fiddle updated: https://jsfiddle.net/308s2ftk/9/

  • Perfect, our was breaking our heads and I decided to ask, thank you

Browser other questions tagged

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