[英]jQuery Datatable on click tr function fires multiple times
當我在表上選擇一行時,然后單擊“添加行”按鈕,將添加一行。 然后,我單擊另一行,然后單擊“添加行”,它將添加兩行。 每次我單擊另一行時,添加的行數也會增加。 我該如何做,所以每次只能添加一行?
我很確定這是由於另一個click事件中的click事件引起的,但是我不確定如何解決該問題。
我當前的事件添加行:
$(document).on('click', 'tr', function() {
var t = $('#data-table').DataTable();
$('#data-table').dataTable();
$('#addbtn').on( 'click', function () {
t.row.add( [
'New Group'
] ).draw( false );
} );
} );
嘗試這個:
$(document).on('click', 'tr', function() {
var t = $('#data-table').DataTable();
$('#data-table').dataTable();
$('#addbtn').on( 'click', function () {
t.row.add( [
'New Group'
] ).draw( false );
//This removes the listener when you are done with it
//And it will create another when you click again
$( "#addbtn").off( "click" );
});
});
或者您基本上可以將addbtn
偵聽器移出tr
偵聽器
希望這些工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.