jQuery - Selector for duplicate ID's

前端 未结 7 1217
名媛妹妹
名媛妹妹 2020-12-02 00:40

I have a page with duplicate ID\'s for a form element. The catch is I the elements show up separately based on a toggle. So both ID\'s never show up simultaneously.

7条回答
  •  半阙折子戏
    2020-12-02 00:58

    The reason this is occurring is because of Duplicate IDs. IDs must be unique for the HTML to be considered valid. Whenever you aren't working against valid HTML, your results are often erratic.

    In this case, even though you are only showing one of the forms at a time, they're both still present in the mark up which is why the last one in the code is always the one that's getting run.

    Since you're using jQuery, I'd highly recommend using classes for this instead.

提交回复
热议问题