gpt4 book ai didi

jquery - JavaScript onclick 事件失败

转载 作者:行者123 更新时间:2023-12-03 22:32:48 26 4
gpt4 key购买 nike

我正在使用 append() 生成 html,然后我试图简单地在单击按钮上发出警报,但失败了。

附加之前我的 JavaScript

$(document).ready(function() {

function testFunction() {
alert("simplty alert");
}

$('#selUsersToSendMail').click(function() {
var selUsersArray = [];

$(".tableBodyConfirmUser1").html('');

$(".selUsersClass:checked").each(function() {
var ids = $(this).attr('value');
$(".tableBodyConfirmUser1").append("<tr id='subsUserId_" + ids + "' class='subsUserId_" + ids + "'>" + $("#selUserId_" + ids).html() + "</tr>");

selUsersArray.push(ids);
});

$(".tableBodyConfirmUser1").append("<tr><td valign='top' colspan='3'><input type='button' style='width:50px;' name='sendMailBtn' id='sendMailBtn' class='sendMailBtn' value='Select' onclick='testFunction();'/></td></tr>");

});

});​

附加后的 HTML

<table id="table-design" border="1" cellspacing="0" cellpadding="0" align="left" width='50%'>
<thead>
<tr>
<th scope="col" id="">Select</th>
<th scope="col" id="">Name</th>
<th scope="col" id="">Email</th>
</tr>
</thead>
<tbody class='tableBodyConfirmUser'>
<tr id='selUserId_3'>
<td valign="top" width="319"><input type="checkbox" name='selUsr' value = "3" class='selUsersClass'/></td>
<td valign="top" width="319"><a href='#' id='name' class='nameClass'>saurav</a></td>
<td valign="top" width="319"><a href='#' id='email'>saurav1214@gmail.com</a></td>
</tr>
<tr id='selUserId_4'>
<td valign="top" width="319"><input type="checkbox" name='selUsr' value = "4" class='selUsersClass'/></td>
<td valign="top" width="319"><a href='#' id='name' class='nameClass'>bhupinder</a></td>
<td valign="top" width="319"><a href='#' id='email'>bhupi@gmail.com</a></td>
</tr>
<tr>
<td valign="top" colspan="3"><input type="button" style='width:50px;'name='sendMailBtn' id='sendMailBtn' value='Select' onclick='testFunction();'/>
</td>
</tr>
</tbody>
</table>

最佳答案

换行

$(".tableBodyConfirmUser1").append("<tr><td valign='top' colspan='3'><input type='button' style='width:50px;' name='sendMailBtn' id='sendMailBtn' class='sendMailBtn' value='Select' onclick='testFunction();'/></td></tr>");

$(".tableBodyConfirmUser1").append("<tr><td valign='top' colspan='3'><input type='button' style='width:50px;' name='sendMailBtn' id='sendMailBtn' class='sendMailBtn' value='Select'/></td></tr>");

即。删除 onclick 并写入:

$(document).ready(function() {

// delegate event handler for upcoming dom element
// aka live event

$('.tableBodyConfirmUser1').on('click', '#sendMailBtn', function() {
alert('Button clicked');
});

// your code
});

关于jquery - JavaScript onclick 事件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946538/

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