2012-07-26 2 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 페이지에서 고유해야합니다. 여기에 코드를 조금 난 그냥 페이지 (당신의 브라우저의 자바 스크립트 콘솔에서 코드를 실행)에 중복 ID 년대를 찾기 위해 쓴 된다 만들면 기본적 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

는 강조 구문을 사용하는 경우에 당신을 두 번 클릭 한 단어를 선택하면 같은 단어의 다른 모든 단어를 강조 표시합니다. 당신은 수작업으로 이름을 바꾸는 작업 (아마도 많은 작업)을해야 할 것입니다. 필드 란 고유 한 이름으로 필드를 만들 수 없기 때문입니다.

+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를 포함하는 배열을 리턴합니다.

관련 문제