2017-09-22 46 views
0

我在jQuery中有一個$POST方法,該方法將id作爲數據發送到我的PHP文件,並使用該ID從數據庫中刪除該行。我的問題是,每次單擊刪除按鈕POST方法被調用多次。

這裏是代碼。

$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function() { 

    var div_id = $(this).closest('[id]').attr('id'); 
    //$(".dash-wrapper .display-class .member-list-div .all-member-box .member-content-shell .modal .modal-content") 
    $(".dash-wrapper .display-class").on('click', '#yes1', function() { 
    $.post("delete.php", { 
     id1: div_id 
    }, function(data) { 
     if (data == 1) { 
     $('#myModal1').modal('hide'); 
     $('body').removeClass('modal-open'); 
     $('.modal-backdrop').remove(); 
     ajaxcall3(); 
     } else { 

     } 
    }) 
    }); 
}); 
+0

你知道,如果沒有任何東西,你不需要別的東西。不是要解決你的問題,它只是爲了節省行數 –

+0

請給我們看看每個*塊,$(「.dash-wrapper .display-class」)將被選中。 – Wndrr

+0

在這裏發佈HTML。 –

回答

1

,每次你點擊了刪除圖像上一次你的AJAX調用是由1周更多的時間。這是因爲您在刪除圖像上的每次點擊都綁定了一個新的事件處理程序,要修復該問題,請在點擊delete-image之前綁定click一次,並使用全局變量獲取id或每次將其重新指定爲以下內容:

$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function() { 

    var div_id = $(this).closest('[id]').attr('id'); 
    $(".dash-wrapper .display-class").off('click', '#yes1'); 
    $(".dash-wrapper .display-class").on('click', '#yes1', function() { 
    $.post("delete.php", { 
     id1: div_id 
    }, function(data) { 
     if (data == 1) { 
     $('#myModal1').modal('hide'); 
     $('body').removeClass('modal-open'); 
     $('.modal-backdrop').remove(); 
     ajaxcall3(); 
     } 
    }) 
    }); 
}); 
+0

OMG bro這個問題修正了!標記爲答案 – Raj

0
 
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function(){ 


}); 


//$(".dash-wrapper .display-class .member-list-div .all-member-box .member-content-shell .modal .modal-content") 
$(".dash-wrapper .display-class").on('click', '#yes1', function(e){ 

var div_id = $(this).closest('[id]').attr('id'); 
    $.post("delete.php", { 
     id1: div_id 
    }, function(data){ 
     if(data==1){ 
     $('#myModal1').modal('hide'); 
     $('body').removeClass('modal-open'); 
     $('.modal-backdrop').remove(); 
     ajaxcall3(); 
     }else{ 
     return false; 
     } 
    } 
) 
    e.preventDefault(); 
}); 

加入該行的點擊功能結束前,並添加元素e 這將停止你點擊的傳播甚至

+0

爲什麼呢?請添加解釋。是什麼讓你相信這會解決他的問題,是什麼使你相信?我們甚至不知道什麼類型的元素他的代碼被稱爲上 – Wndrr

+0

的e.prevent默認是額外的代碼行的單擊事件裏面添加。這並不意味着我重建你以前的代碼。 –

+0

這沒有奏效@Randy Rebucas – Raj

3

.one()

嘗試.one()方法是相同的到.on(),但給定元素和事件類型的處理程序在第一次調用後未被綁定。

例子:

$("#foo").one("click", function() { 
    alert("This will be displayed only once."); 
}); 
+0

不錯** **;) –

0

就在蝙蝠的右下方,你點擊了兩下。這可能是問題嗎?

是否有可能在ajaxcall3()中創建某種循環?

你不是那種每次點擊一次就點擊兩次的人,是嗎?

您的鼠標驅動程序或鼠標本身有問題嗎?

沒有所有的代碼,我只是假設它是愚蠢的,所以嘗試先點擊刪除額外的東西。

 
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function(){ 

    var div_id = $(this).closest('[id]').attr('id'); 

    $.ajax({ 
     type: "POST", 
     url: "delete.php", 
     data: "id1="+div_id, 
     dataType: "text", 
     async: true, 
     success: function(data) { 
      if (data == '1') { 
       $('#myModal1').modal('hide'); 
       $('body').removeClass('modal-open'); 
       $('.modal-backdrop').remove(); 
       ajaxcall3(); 
      } 
     } 
    }); 
}) 

如果不剪,嘗試張貼

 
$(".dash-wrapper .display-class").on('click', '.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image', function(){ 

    var div_id = $(this).closest('[id]').attr('id'); 

    $.ajax({ 
     type: "POST", 
     url: "delete.php", 
     data: "id1="+div_id, 
     dataType: "text", 
     async: true, 
     beforeSend: function() { 
      $('.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image').unbind('click'); 
     }, 
     success: function(data) { 
      if (data == '1') { 
       $('#myModal1').modal('hide'); 
       $('body').removeClass('modal-open'); 
       $('.modal-backdrop').remove(); 
       ajaxcall3(); 
      } 
     }, 
     complete: function() { 
      $('.member-list-div .all-member-box .member-content-shell .single-row .action-column .action-holder .delete-image').bind('click'); 
     } 
    }); 
}); 

我沒有測試在此之前解除綁定click事件,而我只是做這件事,我去。沒有保證它會工作!

編輯 括號問題。

+0

我得到了解決方案,並將其標記爲正確的答案! 謝謝你的時間:) – Raj