簡體   English   中英

如何在jquery中點擊動態創建的html按鈕的id

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

我有幾個(內部循環) 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;\" />");

我想得到我click使用jquerybuttonid

幫我

謝謝

委托使用.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
});

要么

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

或者無論你的sb元素是什么替換身體,因為它存在於dom負載

怎么樣

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

試試這個

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

您可以嘗試以下方法:

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

對動態生成的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