這是我的代碼:如何從數組中取出數組jQuery?
jQuery(document).on('click', '.list li', function(e){
var container = $('#button');
var selectedList = container.attr('data-selected-list');
if(jQuery(this).hasClass('done')){
jQuery(this).removeClass('done');
var myElement = jQuery(this).attr('data-user-id');
var myArray = selectedList;
for (var i = myArray.length - 1; i >= 0; i--) {
if (myArray[i] == myElement) myArray.splice(i, 1);
}
container.attr('data-selected-list', myArray);
} else {
jQuery(this).addClass('done');
if (!selectedList) {
selectedList = [];
} else {
selectedList = JSON.parse(selectedList);
}
selectedList.push(jQuery(this).attr('data-user-id'));
selectedList = JSON.stringify(selectedList);
container.attr('data-selected-list', selectedList);
}
});
我也成功地當.LIST李用戶點擊獲取用戶ID並將其添加爲數組數據選擇列表。
我的問題是,如何解除從數據選擇列表(數組列表)中刪除用戶ID?
我在stackoverflow上搜索,但我沒有做到這一點。
使用'indexOf()'查找數組中元素的索引,'splice()'從數組中移除元素。 – Barmar
@Barmar我嘗試,但我失敗了,你可以通過添加最終答案來幫助我嗎? –
對不起,無法發佈解決問題的答案。如果你編輯的問題,以顯示你嘗試過什麼,我可以重新打開它,並張貼與校正的答案。 – Barmar