jQuery id selector works only for the first element

后端 未结 7 2091
借酒劲吻你
借酒劲吻你 2020-11-22 02:55

I have 3 buttons with same id, I need to get each button value when he\'s being clicked.

7条回答
  •  无人共我
    2020-11-22 03:21

    Although changing the id's to a class is better, you can get all the elements with the same id using the attribute equals selector:

    $('[id="xyz"]')
    

    Or this to get only buttons with id xyz:

    $('button[id="xyz"]')
    

    Or divs with id xyz:

    $('div[id="xyz"]')
    

    etc.

    Alternatively you could use the "Attribute Contains Selector" to get all elements with ids that contain "xyz":

    $('[id*="xyz"]')
    

    Of course, this means all elements with id that partially contain "xyz" will get selected by this.

提交回复
热议问题