2016-01-16 48 views
0

我有一個頁面上加載模式的按鈕。jquery刪除舊功能,並在點擊處理程序

在這種模式下,我顯示數據。

該數據可以包含例如某個表的5個條目 當條目被刪除時,數據被刷新。添加條目時也是如此。

現在,當我通過單擊外部並重新打開它來關閉模式時。第一個模態附加的功能仍然存在並且正在觸發。 所以,如果我添加一個條目。這樣做的ajax和腳本被解僱/被調用兩次。

代碼:

  $(document).on('click','.remove_team',function(){ 
       var tournament_id = $(this).data('tourn'); 
       $.ajax({ 
        url :'./organisations/waiting_list/team_assignement.php', 
        type :'POST', 
        data : { 
         action : 'remove_team', 
         team_id : $(this).data('team'), 
         tourn : $(this).data('tourn') 
        }, 
        success : function(){ 
         LoadParticipantList(tournament_id); 
        } 
       }) 
      }) 
      function LoadParticipantList(tournament){ 
       $.ajax({ 
        url :'./organisations/waiting_list/team_assignement.php', 
        type :'POST', 
        data : { 
         action :'fetch_team_participants', 
         tournament : tournament 
        },success : function(res){ 
         res = jQuery.parseJSON(res);   
         $("#counter_"+tournament).html(res.count);    
         $("#div_"+tournament).html(res.teams); 
        } 
       }) 
      } 

的是換掉一個單擊處理程序的正確方法,然後再直接添加它,以便在AJAX火災?

+0

兩個ajax調用的目的是什麼? – guest271314

+0

哪裏是模態代碼?你所展示的只是ajax代碼 – charlietfl

回答

0

你可以做的是增加你的模式彈出外的單擊事件。這個點擊事件停止,你不希望有治療的...

$('documents').bind('click', function(e) { 
    if($(e.target).closest('#your_modal_id').length == 0) { 
     //click happened outside the modal popup : stopping treatments 
    } 
}); 

還添加了停止傳播事件,以避免不必要的治療方法來運行...

$('#your_modal_id').click(function(event){ 
event.stopPropagation(); 
}); 

希望它可以幫助

0

jQuery .off方法將完成這項工作。

相關問題