How to prevent ajax call from firing twice

问题: I have an ajax call which is fired when a click event occurs on document. The ajax call seems to be executing twice and I am not sure why this happens. This is running on...

问题:

I have an ajax call which is fired when a click event occurs on document. The ajax call seems to be executing twice and I am not sure why this happens.

This is running on MySQL 5, PHP 5 and Apache 2

My question is : How can I prevent ajax call from firing twice?

Here is my JQuery code:

$(document).on('click', '.edit_quyet_dinh', function(){  
    var ma_quyet_dinh = $(this).attr("id");
    var ma_cong_trinh = $(this).attr("data-id");
    $.ajax({  
        url:"fetch_quyet_dinh.php",  
        method:"POST",  
        data:{ma_quyet_dinh:ma_quyet_dinh,ma_cong_trinh:ma_cong_trinh},  
        dataType:"json",  
        success:function(data){  
            $('#ten_quyet_dinh').val(data.ten_cong_trinh);  
            $('#shct_qd').val(data.so_hieu);  
            $('#so_quyet_dinh').val(data.so_quyet_dinh);  
            $('#ngay_duyet').val(data.ngay_duyet);  
            $('#ma_quyet_dinh').val(data.ma_quyet_dinh);  
            $('#insert_qd').val("Update");  
            $('#add_quyet_dinh').modal('show');  
        }  
    });  
});     

回答1:

You must use this code:

$(document).on('click', '.edit_quyet_dinh', function(event){  
   event.preventDefault();
   event.stopPropagation();

   // other codes
});  

this method stopped twice call jquery event function.

stopPropagation stops the event from bubbling up the event chain.

preventDefault prevents the default action the browser makes on that event.


回答2:

I got it. Thank everyone so much

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章