Getting sender in jQuery

后端 未结 2 483
猫巷女王i
猫巷女王i 2021-01-07 16:41

I have a large list of check boxes all with unique id values. I\'d like to get the id of the checkbox that executes my JavaScript function. Can I d

相关标签:
2条回答
  • 2021-01-07 17:04

    You can get the target of the event using event.target.

    $('input:checkbox[id]').change(function(event) {
      var checkboxID = $(event.target).attr('id');
      alert(checkboxID);
    });
    

    JSfiddle Demo

    0 讨论(0)
  • 2021-01-07 17:13

    If this points to your checkbox, then you would get the id using $(this).attr('id')

    For example:

    $('input:checkbox').click(function(){
        var id = $(this).attr('id');
        // do something with id
    });
    

    See DEMO.

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