mercredi 18 novembre 2020

How to fix onclick listener on ajax?

When I click .delete, and close the modal, then click another delete, then click #btn-submit, it send ajax 2 times. How to fix this?

$(document).on('click', '.delete', function(){
        //some code here
        $.ajax({
            url:'ajax.php',
            type:'POST',
            cache:false,
            dataType:'xml',
            data:{
                data:'get_article',
                id:$(this).attr('article-id')
            },
            success:function(r){
                //some code here
                $(document).on('click', '#delete-modal #btn-submit', function(){
                    $.ajax({
                        url:'ajax.php',
                        type:'POST',
                        data:{
                            data:'delete_article',
                            id:$(r).find('id').text()
                        },
                        cache:false,
                        success:function(r){
                            //some code here
                        }
                    });
                });
            }
        });

Aucun commentaire:

Enregistrer un commentaire