reusing modal fires multiple times

2019-08-05 17:25发布

问题:

I'm using the twitter bootstrap modal plugin. The submit from the modal submits data to the server.

For some reason, when I open and submit the modal multiple times the submit is firing as many times as the modal has been opened previously.

So if I click "update" on a row. Edit and save, it fires once since that's the first attempt. If I then click "update" on a second row. Edit, and save, it fire's twice. 3rd time fires 3 times, 4 fires 4. So on, so forth.

I've been trying to resolve this for days now and have found nothing. Some guidance would be leave me eternally grateful.

回答1:

This is how I have fixed this issue:

I have a global variable delete_id=0 just after the <script> tag to store the ID which I want to delete (this can be dynamic to use server side script). I have also added ID myModalAction for the Delete button in my modal HTML.

I have called the modal this way:

$('.delete_row').click(function (){
    delete_id = $(this).attr('id');
    $('#myModal').modal();
});

And for myModalAction:

$('#myModalAction').click(function(e){
    $('#'+delete_id).remove();
    $('#myModal').modal('hide');
});

Complete working code can be found here.