Javascript 在 jQuery 中获取发件人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5287759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:29:10  来源:igfitidea点击:

Getting sender in jQuery

javascriptjquery

提问by esqew

I have a large list of check boxes all with unique idvalues. I'd like to get the idof the checkbox that executes my JavaScript function. Can I do this using $(this)?

我有一个很大的复选框列表,所有复选框都具有唯一id值。我想获取id执行我的 JavaScript 函数的复选框的 。我可以这样做$(this)吗?

回答by Andrew Moore

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

您可以使用event.target.

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

JSfiddle Demo

JSfiddle 演示

回答by rsp

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

如果this指向您的复选框,那么您将使用$(this).attr('id')

For example:

例如:

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

See DEMO.

演示