How to prevent events from being bound multiple times

后端 未结 5 1380
后悔当初
后悔当初 2021-01-01 15:10
 $(\'.ajax\').click
 (        
    function()
    {
        // If been bound then we need to return here.
        alert(\':D\');
    }
 )

 $(\'.ajax\').click
 (
            


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 15:58

    function demo()
    {
    // your code here which will called on click event
    }
    
    $(document).ready(function(){
    $('.add').bind('click',demo);
    
    });
    
    //After successfully ajax call response
    //Unbind the click event first
    $('.add').unbind('click',demo);
    //Then again bind the event
    $('.add').bind('click',demo);
    

提交回复
热议问题