2012-07-26 3 views
9

私は約800フィールドのフォームを作成しました。無意識のうちに、フォーム内のいくつかのフィールドに同じIDを与えました。それらをトレースする方法?フォーム内に重複するIDを見つけるにはどうすればいいですか?

+7

クイックソリューション:http://validator.w3.org/ – Simone

+0

詳細を入力してください。あなたのHTMLフォームはどのような形式ですか? –

+0

notepad ++またはtext wranglerを見つけて交換してください。 –

答えて

13

http://validator.w3.org/が便利なソリューションになります。

//See your console for duplicate ids 

$('[id]').each(function(){ 
    var id = $('[id="'+this.id+'"]'); 
    if(id.length>1 && id[0]==this) { 
    console.log('Duplicate id '+this.id); 
    alert('duplicate found'); 
    } 
}); 

・ホープ、このことができます:しかし、あなたはこのような何かを行うことができますjqueryのを使用。

2

これは、2011年

は時々我々は要素のIDを忘れてしまったように見える、5月6日にEnekoアロンソによって書かれたHTMLページ

に重複したIDを見つけるあなた

Source: Finding duplicate ID’s on an HTML page

を助けるかもしれません は、HTMLページ上で一意であることを意図しています。ここでは、コードを少しは私はちょうどページ(あなたの ブラウザのJavaScriptコンソール上でコードを実行する)に重複したIDを見つけるために書いた さ:defualt Notepad++ことで

var idList = {}; 
var nodes = document.getElementsByClassName(''); 
for (var i in nodes) { 
    if (!isNaN(i) && nodes[i].id) { 
    idList[nodes[i].id] = idList[nodes[i].id]? idList[nodes[i].id]+1:1; 
    } 
} 
for (var id in idList) { 
    if (idList[id] > 1) console.log("Duplicate id: #" + id); 
} 
+0

これは 'var nodes = document 'という行が編集できるようにするだけです。 –

+0

@Joey私は自分の答えを編集しました。私はそれを心に留めておきます。 – Christos312

-2

は、シンタックスハイライトを持っている場合、ダブルクリックそれを選択する1つの単語、同じ単語の他のすべての発生を強調表示します。あなたは手作業で名前を変更する(おそらくたくさんの)作業をやらなければならないでしょう。フィールドは自分の一意の名前を思いつくことができないからです。

+0

それは約1600回のクリックになります。 .. – Joey

+0

よく始まります。彼は重複がどこにあるのか、いくつか考えなければならない。 –

1

例を見てみると、ページ上のフォーム/要素内のすべての重複IDが検出され、重複したID名がコンソールに出力されます。

アレイはthis postから取得されました。

<html> 
    <body> 
     <form id="frm"> 
      <input type="text" id="a" /> 
      <input type="text" id="b" /> 
      <input type="text" id="c" /> 
      <input type="text" id="d" /> 
      <input type="text" id="e" /> 
      <input type="text" id="f" /> 
      <input type="text" id="a" /> 
      <input type="text" id="h" /> 
      <input type="text" id="i" /> 
      <input type="text" id="j" /> 
      <input type="text" id="d" /> 
      <input type="text" id="l" />    
     </form> 
    </body> 
    <script type="text/javascript"> 
     Array.prototype.contains = function(obj) { //Add a 'contains' method to arrays 
      var i = this.length; 
      while (i--) { 
       if (this[i] === obj) { 
        return true; 
       } 
      } 
      return false; 
     } 

     frm = document.getElementById('frm'); //Get the form 
     els = frm.getElementsByTagName('input'); //Get all inputs within the form 

     ids = new Array(els.length); //Create an array to hold the IDs 

     for(e = 0; e < els.length; e++) { //Loop through all of the elements 
      if(ids.contains(els[e].id)) //If teh array already contains the ID we are on 
       console.log('Duplicate: '+els[e].id); //Print 'Duplicate: {ID}' to the console 

      ids.push(els[e].id); //Add the ID to the array 
     } 

    </script> 
</html> 

上記のコードが出力さ以下:

重複:

重複:D

1

ワンっぽいライナー使用だけアレイ方法:

[].map.call(document.querySelectorAll("[id]"), 
    function (e) { 
     return e.id; 
    }).filter(function(e,i,a) { 
     return ((a.lastIndexOf(e) !== i) && !console.log(e)); 
    }) 

すべての重複をログに記録し、見つかった場合はIDを含む配列を戻します。

関連する問題