jQuery id selector works only for the first element

后端 未结 7 1994
借酒劲吻你
借酒劲吻你 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:34

    If you have same id in a container you can use on() to access each element for every event

    $("#containers").on("click","#xyz",function(){
      alert($(this).val())
      })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div id="containers">
    
    <button id="xyz" type="button" class="btn btn-primary" value="1">XYZ1</button>
    <button id="xyz" type="button" class="btn btn-primary" value="2">XYZ2</button>
    <button id="xyz" type="button" class="btn btn-primary" value="3">XYZ3</button>
    
    </div>

    and info about on() is here

    0 讨论(0)
提交回复
热议问题