javascript - jQuery - 动态添加按钮单击不起作用

标签 javascript jquery jquery-ui jquery-dialog

我编写了以下代码,创建了一个带有动态添加内容的弹出窗口。现在我想删除这些添加的项目或编辑它们,但单击这些按钮之一似乎没有触发事件(btnLSM_Remove + btnLSM_Edit)。有任何线索为什么会这样吗? btnLSM_AddbtnLSM_Okay 的工作方式相同,而且它们确实有效...

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

最佳答案

调用时您的 btnLSM_Remove 按钮不存在

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

因此,$('#btnLSM_Remove') 集合为空,并且处理程序未添加任何内容。

您可以使用 on功能:

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

注册一个处理程序,该处理程序将应用于出现在委托(delegate)定义之后的按钮。

编辑:

在 jQuery 1.6.2 中,您可以使用 live :

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

关于javascript - jQuery - 动态添加按钮单击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12732917/

相关文章:

jquery获取ajax加载文本框的值

javascript - 如何使用 Angular 在 JQueryUI Accordion 中填充数据?

jquery-ui - Selenium Test 测试 Jquery 效果(滑动、淡入淡出)

jquery - 在 WordPress 主题选项页面中排队 jquery-ui 元素

javascript - Youtube API 只搜索视频

javascript - 是否可以声明自己的 typescript 模块并像 npm 包一样导入它们

javascript - 加载 jQuery UISlider 时显示默认值

javascript - 在 div 中固定位置背景图像

javascript - 访问 JSON 元素返回未定义

javascript - facebook 未在 iframe 内加载