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

时间:2021-11-29 20:38:20

I have several(inside loop) dynamically created html buttons like :

我有几个(内部循环)动态创建的html按钮,如:

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

我想得到我点击使用jquery的按钮的ID

Help Me

Thanks

5 个解决方案

#1


3  

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负载

#2


1  

How about

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

#3


1  

Try this

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

#4


1  

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")
        }
    });

#5


0  

Use 'live' event for dynamically generated html element's event

对动态生成的html元素事件使用'live'事件

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

#1


3  

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负载

#2


1  

How about

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

#3


1  

Try this

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

#4


1  

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")
        }
    });

#5


0  

Use 'live' event for dynamically generated html element's event

对动态生成的html元素事件使用'live'事件

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