web-dev-qa-db-ja.com

jQuery:重複するIDを検索し、最初のIDを除くすべてを削除する

_    $('[id]').each(function () {

        var ids = $('[id="' + this.id + '"]');

        // remove duplicate IDs
        if (ids.length > 1 && ids[0] == this) $('#' + this.id).remove();

    });
_

上記は最初の重複IDを削除しますが、最後のIDを削除したいと思います。 $('#'+ this.id + ':last')を試しましたが、役に立ちませんでした。

フィドル

フィドルでは、追加アクションが発生するときに、値が「sample」の入力を保持する必要があります。

13
ditto

Jquery filter :gt(0)を使用して、最初の要素を除外します。

_$('[id]').each(function () {
    $('[id="' + this.id + '"]:gt(0)').remove();
});
_

または、使用可能なすべての要素を選択してから、.slice(1)を使用して最初の要素を除外します。

_$('[id]').each(function (i) {
    $('[id="' + this.id + '"]').slice(1).remove();
});
_
21
novalagung

試してください:

 $('[id="' + this.id + '"]:not(#" + this.id + ":first)').remove();
4
Mahesh Reddy

あなたが試すことができます

$("#ID").nextAll().remove();
3
user2235386
$('[id]').each(function() {
   var $ids = $('[id=' + this.id + ']');
   if ($ids.length > 1) {
     if(this.id === your_id)//which is duplicating
         $ids.not(':first').remove();
     }
});
2
user2254898

これを試して

var duplicated = {};

$('[id]').each(function () {   

    var ids = $('[id="' + this.id + '"]');

    if ( ids.length <= 1 ) return  ;

    if ( !duplicated[ this.id ] ){
         duplicated[ this.id ] = [];   
    }       

    duplicated[ this.id ].Push( this );

});

// remove duplicate last ID, for elems > 1 
for ( var i in duplicated){

    if ( duplicated.hasOwnProperty(i) ){  

             $( duplicated[i].pop() ).remove();            
    }
}

jsfiddleは http://jsfiddle.net/z4VYw/5/

1
rab

このコードは他のコードよりも長いですが、二重にネストされたループはその動作を明確にするはずです。

このアプローチの利点は、DOMをスキャンしてid属性を持つ要素のリストを生成するだけでよいことですonce次に、同じリストを使用して検索(および削除)します重複。

すでに削除された要素にはparentNode === nullがあるため、配列を反復処理するときにスキップできます。

var $elems = $('[id]');
var n = $elems.length;

for (var i = 0; i < n; ++i) {
    var el = $elems[i];
    if (el.parentNode) {  // ignore elements that aren't in the DOM any more
        var id = el.id;
        for (var j = i + 1; j < n; ++j) {
            var cmp = $elems[j];
            if (cmp.parentNode && (cmp.id === id)) {
                $(cmp).remove();  // use jQuery to ensure data/events are unbound
            }
        }
    }
}
1
Alnitak

not()を使用して現在の要素をidsから削除し、残りを削除します。

フィドル

$('body').on("click", ".placeholder", function() {

    data = '<section id="e1"><input name="e1name" /></section><section id="two"><input name="two" value="two section" /></section>';

    $('form').append(data);

        // ideally I'd like to run this function after append but after googling I find that's not possible.
        // for each ID ...
        $('[id]').each(function () {

            var ids = $('[id="' + this.id + '"]');

            if (ids.length>1) {                    
                ids.not(this).remove();            
            }

            return;

        });

});
0
Jon Egerton

以下の関数を使用してみてください私にとっては問題なく動作します:

 $('#favourities-ajax ul li').each(function(i) {
      $('[id="' + this.id + '"]').slice(1).remove();
      });
0
Vignesh Ben