gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:38:26 24 4
gpt4 key购买 nike

我编写了以下代码,创建了一个带有动态添加内容的弹出窗口。现在我想删除这些添加的项目或编辑它们,但单击这些按钮之一似乎没有触发事件(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com