How to find duplicate id's in a form?

后端 未结 6 2573
无人及你
无人及你 2021-02-20 13:16

I\'ve created a form with about 800 fields in it. Unknowingly I\'ve given same id for few fields in the form. How to trace them?

6条回答
  •  囚心锁ツ
    2021-02-20 13:39

    This might help you

    Source: Finding duplicate ID’s on an HTML page

    Finding duplicate ID’s on an HTML page

    Written by Eneko Alonso on May 6, 2011

    Looks like sometimes we forgot element ID’s are meant to be unique on a HTML page. Here is a little bit of code I just wrote to find duplicate ID’s on a page (run the code on your browser’s javascript console):

    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);
    }
    

提交回复
热议问题