最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - dynamic added button click not working - Stack Overflow

programmeradmin2浏览0评论

I wrote the following code creating a popup with dynamic added content. Now I want to delete these added items or edit them, but it didn't seem to fire an event clicking one of those buttons (btnLSM_Remove + btnLSM_Edit). Any clues why it is so? btnLSM_Add and btnLSM_Okay work the same way and they do work...

function ListManagementDialog(obj, dialogTitle, dialogText, listDelimiter, btnNames) {
    if (!$.isArray(btnNames)) {        
        return false;
    }

     if (dialogConfirmed) {
        return false;
    }

    btns[btnNames[0]] = function () {
        $(this).dialog('close');
        dialogConfirmed = true;
        if (obj) {
            obj.click();
        }
    };

    btns[btnNames[1]] = function () {
        $(this).dialog('close');
    };



    $('body').append(String.Format('<div id="divLSM_Dialog" title="{0}"><p>{1}</p>' +
        '<button id="btnLSM_Add" class="btnAdd" type="button" role="button" aria-disabled="false" title="Hinzuf&#252;gen" />' +               
        '<input id="txbLSM_Emailadresse" class="text ui-widget-content ui-corner-all" type="text" name="txbLSM_Emailadresse" style="display:none;">' +
        '<button id="btnLSM_Okay" class="btnOkay" type="button" role="button" aria-disabled="false" title="&#220;bernehmen" style="display:none;" />' +
        '<br /><br />' +
        '<table id="tblLSM_Items" class="ui-widget ui-widget-content">' +
                 '<thead>' +
                        '<tr class="ui-widget-header ">' +
                '<th>Emailadresse</th>' +
                '<th />' +
                '</tr>' +
            '</thead>' +
            '<tbody />' +
        '</table>' +
        '</div>', dialogTitle, dialogText));

    $('#btnLSM_Add').click(function () {
        $('#txbLSM_Emailadresse').val('');
        $('#txbLSM_Emailadresse').show();
        $('#btnLSM_Okay').show();
        $('#txbLSM_Emailadresse').focus();
    });  

    $('#btnLSM_Okay').click(function () {
        $('#tblLSM_Items tbody').append('<tr>' +
            '<td>' + $('#txbLSM_Emailadresse').val() + '</td>' +
            '<td>' + '<button id="btnLSM_Remove" class="btnRemove" type="button" role="button" aria-disabled="false" title="Entfernen" />' + '<button id="btnLSM_Change" class="btnEdit" type="button" role="button" aria-disabled="false" title="&#196;ndern" />' + '</td>' +
            '</tr>');

        $('#txbLSM_Emailadresse').hide();
        $('#btnLSM_Okay').hide();
    });

    $('#btnLSM_Remove').click(function () {
        alert("hohoho"); //no alert-popup
    });   

     $('#btnLSM_Change').click(function () {
        alert("hohoho"); //no alert-popup
    });

    $('#divLSM_Dialog').dialog({
        modal: true,
        resizable: false,
        draggable: true,
        width: 600,
        height: 300,
        close: function (event, ui) {
            $('body').find('#divLSM_Dialog').remove();
        },
        buttons: btns
    });

    return dialogConfirmed;
}

I wrote the following code creating a popup with dynamic added content. Now I want to delete these added items or edit them, but it didn't seem to fire an event clicking one of those buttons (btnLSM_Remove + btnLSM_Edit). Any clues why it is so? btnLSM_Add and btnLSM_Okay work the same way and they do work...

function ListManagementDialog(obj, dialogTitle, dialogText, listDelimiter, btnNames) {
    if (!$.isArray(btnNames)) {        
        return false;
    }

     if (dialogConfirmed) {
        return false;
    }

    btns[btnNames[0]] = function () {
        $(this).dialog('close');
        dialogConfirmed = true;
        if (obj) {
            obj.click();
        }
    };

    btns[btnNames[1]] = function () {
        $(this).dialog('close');
    };



    $('body').append(String.Format('<div id="divLSM_Dialog" title="{0}"><p>{1}</p>' +
        '<button id="btnLSM_Add" class="btnAdd" type="button" role="button" aria-disabled="false" title="Hinzuf&#252;gen" />' +               
        '<input id="txbLSM_Emailadresse" class="text ui-widget-content ui-corner-all" type="text" name="txbLSM_Emailadresse" style="display:none;">' +
        '<button id="btnLSM_Okay" class="btnOkay" type="button" role="button" aria-disabled="false" title="&#220;bernehmen" style="display:none;" />' +
        '<br /><br />' +
        '<table id="tblLSM_Items" class="ui-widget ui-widget-content">' +
                 '<thead>' +
                        '<tr class="ui-widget-header ">' +
                '<th>Emailadresse</th>' +
                '<th />' +
                '</tr>' +
            '</thead>' +
            '<tbody />' +
        '</table>' +
        '</div>', dialogTitle, dialogText));

    $('#btnLSM_Add').click(function () {
        $('#txbLSM_Emailadresse').val('');
        $('#txbLSM_Emailadresse').show();
        $('#btnLSM_Okay').show();
        $('#txbLSM_Emailadresse').focus();
    });  

    $('#btnLSM_Okay').click(function () {
        $('#tblLSM_Items tbody').append('<tr>' +
            '<td>' + $('#txbLSM_Emailadresse').val() + '</td>' +
            '<td>' + '<button id="btnLSM_Remove" class="btnRemove" type="button" role="button" aria-disabled="false" title="Entfernen" />' + '<button id="btnLSM_Change" class="btnEdit" type="button" role="button" aria-disabled="false" title="&#196;ndern" />' + '</td>' +
            '</tr>');

        $('#txbLSM_Emailadresse').hide();
        $('#btnLSM_Okay').hide();
    });

    $('#btnLSM_Remove').click(function () {
        alert("hohoho"); //no alert-popup
    });   

     $('#btnLSM_Change').click(function () {
        alert("hohoho"); //no alert-popup
    });

    $('#divLSM_Dialog').dialog({
        modal: true,
        resizable: false,
        draggable: true,
        width: 600,
        height: 300,
        close: function (event, ui) {
            $('body').find('#divLSM_Dialog').remove();
        },
        buttons: btns
    });

    return dialogConfirmed;
}
Share Improve this question edited Oct 4, 2012 at 17:55 Ram 145k16 gold badges172 silver badges200 bronze badges asked Oct 4, 2012 at 17:52 UNeverNoUNeverNo 5833 gold badges8 silver badges32 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

Your btnLSM_Remove button doesn't exist when you call

$('#btnLSM_Remove').click(function () {

So the $('#btnLSM_Remove') collection is empty and the handler is added to nothing.

You may use the on function:

$('#divLSM_Dialog').on('click', '#btnLSM_Remove', function () {

to register an handler that will apply to a button appearing after the delegation definition.

EDIT :

In jQuery 1.6.2, you may use live :

$('#btnLSM_Remove').live('click', function () {
发布评论

评论列表(0)

  1. 暂无评论