jQuery - Dynamically Create Button and Attach Event Handler

前端 未结 5 1073

I would like to dynamically add a button control to a table using jQuery and attach a click event handler. I tried the following, without success:

$(\"#myBut         


        
5条回答
  •  感情败类
    2020-12-13 13:06

    Your problem is that you're converting the button into an HTML snippet when you add it to the table, but that snippet is not the same object as the one that has the click handler on it.

    $("#myButton").click(function () {
        var test = $('').click(function () {
            alert('hi');
        });
    
        $("#nodeAttributeHeader").css('display', 'table-row'); // NB: changed
    
        var tr = $('').insertBefore('#addNodeTable tr:last');
        var td = $('').append(test).appendTo(tr);
    });
    

提交回复
热议问题