简体   繁体   English

如何在jquery中点击动态创建的html按钮的id

[英]How to get the id of a dynamically created html button on its click in jquery

I have several(inside loop) dynamically created html buttons like : 我有几个(内部循环) dynamically创建的html buttons如:

sb.Append("<input type=\"button\"  name=\"deleteimage" + id 
              + " \" id=\"btndelete" + id 
              + "\" value=\"Delete\" class=\"t-button t-grid-delete\" " 
              + " style=\"margin-left:10px;\" />");

and I want to get the id of the button on which I click using jquery 我想得到我click使用jquerybuttonid

Help Me 帮我

Thanks 谢谢

delegate using .on() (jQuery 1.7+) or .delegate() (jQuery 1.6 and lower) 委托使用.on() (jQuery 1.7+)或.delegate() (jQuery 1.6及更低版本)

$('body').on('click','input[type=button]',function(){
     alert(this.id); // <-- this refers to current clicked input button
});

or 要么

$('body').delegate('input[type=button]','click',function(){
     alert(this.id);
});

Or whatever your sb element is replace body with that since it exists on dom load 或者无论你的sb元素是什么替换身体,因为它存在于dom负载

How about 怎么样

$('input:button').click(function(){
    alert('Clicked ' + this.id);

Try this 试试这个

$("input[type=button]").click(function()
{
    alert(this.id);
});

You can try the following: 您可以尝试以下方法:

$(document).click(function(event) { 
        var target = event.target;      
        if ($(target).hasClass("t-grid-delete")) { // check if element has class t-grid-delete is your button
            var targetId =  $(target).attr("id");
            // set your image id with attribute like image_id
            // it take easy to get image id $(target).attr("image_id")
        }
    });

Use 'live' event for dynamically generated html element's event 对动态生成的html元素事件使用'live'事件

$("input[type=button]").live('click',function()
{
    alert(this.id);
});

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

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