简体   繁体   English

如何获取动态创建的复选框javascript jquery的ID

[英]How to get ID of dynamically created checkbox javascript jquery

i created checkbox dynamically and using classname im able to access all. 我动态创建了复选框,并使用可以访问所有的类名。

code

$('.checkboxclass').each(function()
{

});

now i want to get id of each checkbox. 现在我想得到每个复选框的id。 how to the same any idea suggestion ? 如何相同的任何想法建议?

$('.checkboxclass').each(function()
{
alert($(this).attr('id'));
});

You can always get any attribute of an HTML element by name using the attr method of the jQuery object. 您始终可以使用jQuery对象的attr方法按名称获取HTML元素的任何属性

eg To retrieve the id of an element: 例如,要检索元素的id:

$(elem).attr('id');

Within the context of the jQuery each method, you also have access to the underlying DOM element using the keyword this . 在jQuery each方法的上下文中,您还可以使用关键字this访问底层DOM元素。 So you can also retrieve the id using native JS: 所以你也可以使用原生JS检索id:

this.id;

Try the following ( see jsfiddle ): 尝试以下( 请参阅jsfiddle ):

$('.checkboxclass').each(function() {
    alert(this.id);
});

Use this 用这个

$(this).attr('id');

in your square brackets. 在方括号中。

Javascript 使用Javascript

this.id;

jQuery jQuery的

$(this).attr('id');

or 要么

$(this).prop('id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM