使用jQuery比较并从一个List中删除重复的项目

时间:2016-11-03 14:08:48

标签: jquery html-lists

我正在比较2个列表中的项目,我需要从主列表中删除该元素( #localInvCountryList

 $('#localInvCountryList li').each(function () {
                var txt = $(this).text();
                if (duplicate[txt])
                    $(this).remove();
                else                    

                 // Check duplicate countries in currentRegCountryList
                $('#currentRegCountryList li').each(function () {
                    var txt2 = $(this).text();
                    if (txt == txt2) {
                        console.log("Duplicaded");
                        //Remove from localInvCountryList
                       $('#currentRegCountryList li').remove(); // this removes the entire list instead of the repeated element!  
                    }
                })
                    // end check
                duplicate[txt] = true;
            });

2 个答案:

答案 0 :(得分:1)

使用$(this).remove()代替$('#currentRegCountryList li').remove();

要从localInvCountryList中删除,请按文字查找并删除。

$('#localInvCountryList li').filter(function(){
    return $(this).text() === txt;
}).remove();

答案 1 :(得分:0)

我设法用:

$('#localInvCountryList li:contains('+txt+')').remove();