reusing modal fires multiple times

2019-08-05 17:39发布

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条回答
冷血范
2楼-- · 2019-08-05 18:11

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.

查看更多
登录 后发表回答