刪除操作按鈕,當我有一個建立使用數據表的表的頁面,並且每行有一個操作按鈕,其將從該行一些數據(一個ID,只是一個int)到列表。爲了防止用戶多次點擊每個操作按鈕,我做了這樣的操作,因此第一次點擊它時刪除了一行按鈕。這很好,直到我意識到按鈕回來,如果你切換到表的下一頁,然後切換回第一頁。古怪的行爲從表中的JavaScript
爲了解決這個問題,我寫了一些JS來比較每列與我的數據的列表,如果該行的數據已經在列表中,它會刪除該行的操作按鈕。每次使用DataTable的draw.dt
函數重新加載表格(例如,當您切換到表格的不同頁面並返回時),都會發生這種情況。
問題是,當我切換頁面和JavaScript去工作刪除按鈕,出於某種原因它跳過隔行。例如,如果我點擊(因此刪除)前三行的動作按鈕,切換頁面並切換回來,我的JS將刪除第1,3,5行上的動作按鈕,而不是1,2和3行。一致的是,由於我的草率的JS技能,它必須是某種簡單的循環錯誤,但我找不到它。
下面是代碼:
$(document).on('draw.dt', function() {
var $allSerialNumbers = $('.box-electrode'); // This is a list of all the rows' action buttons/ID's
$('#electrodes li').each(function (i, li) { // This is the list of ID's I'm comparing to
var $id = $(li).data('id');
for(var index = 0; index < $allSerialNumbers.length; index++){
if($id == $allSerialNumbers.eq(index).attr('data-id')){ // For each item in my ID list, run through all the rows and see if any Action Button ID's match any on the list
$('.box-electrode').eq(index).remove(); // If they do match, remove that Action Button
}
}
})
});
插入'的console.log($ ID + '== '+ $ allSerialNumbers.eq(指數).attr(' 數據-ID');''的爲()'循環之後,看看日誌怎麼說它。可能是在那裏,這是造成問題。發佈JFiddle與信息,我可以採取更深入的瞭解。 – Matthew