所以我有这个按钮,它会在表格中添加一个新行,但我的问题是它.new_participant_form在append方法发生后不再监听click事件.
单击"添加新条目",然后单击"表单"名称.
$('#add_new_participant').click(function() {
var first_name = $('#f_name_participant').val();
var last_name = $('#l_name_participant').val();
var role = $('#new_participant_role option:selected').text();
var email = $('#email_participant').val();
$('#registered_participants').append('<tr><td><a href="#" class="new_participant_form">Participant Registration</a></td><td>'+first_name+ ' '+last_name+'</td><td>'+role+'</td><td>0% done</td></tr>');
});
$('.new_participant_form').click(function() {
var $td = $(this).closest('tr').children('td');
var part_name = $td.eq(1).text();
alert(part_name);
});
});
Run Code Online (Sandbox Code Playgroud)
<table id="registered_participants" class="tablesorter">
<thead>
<tr>
<th>Form</th>
<th>Name</th>
<th>Role</th>
<th>Progress </th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#" class="new_participant_form">Participant Registration</a></td>
<td>Smith Johnson</td>
<td>Parent</td>
<td>60% done</td>
</tr>
</tbody>
</table>
<button id="add_new_participant"></span>Add New Entry</button>
Run Code Online (Sandbox Code Playgroud)
Den*_*ret 185
使用上:
$('#registered_participants').on('click', '.new_participant_form', function() {
Run Code Online (Sandbox Code Playgroud)
因此,即使在绑定事件处理程序之后添加了click,也会将click委托给#registered_participants拥有该类的任何元素new_participant_form.
Rok*_*jan 29
该.on()方法用于将事件委托给元素,动态添加或已经存在于DOM中:
// STATIC-PARENT on EVENT DYNAMIC-CHILD
$('#registered_participants').on('click', '.new_participant_form', function() {
var $td = $(this).closest('tr').find('td');
var part_name = $td.eq(1).text();
console.log( part_name );
});
$('#add_new_participant').click(function() {
var first_name = $.trim( $('#f_name_participant').val() );
var last_name = $.trim( $('#l_name_participant').val() );
var role = $('#new_participant_role').val();
var email = $('#email_participant').val();
if(!first_name && !last_name) return;
$('#registered_participants').append('<tr><td><a href="#" class="new_participant_form">Participant Registration</a></td><td>' + first_name + ' ' + last_name + '</td><td>' + role + '</td><td>0% done</td></tr>');
});Run Code Online (Sandbox Code Playgroud)
<table id="registered_participants" class="tablesorter">
<thead>
<tr>
<th>Form</th>
<th>Name</th>
<th>Role</th>
<th>Progress </th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#" class="new_participant_form">Participant Registration</a></td>
<td>Smith Johnson</td>
<td>Parent</td>
<td>60% done</td>
</tr>
</tbody>
</table>
<input type="text" id="f_name_participant" placeholder="Name">
<input type="text" id="l_name_participant" placeholder="Surname">
<select id="new_participant_role">
<option>Parent</option>
<option>Child</option>
</select>
<button id="add_new_participant">Add New Entry</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)
阅读更多:http://api.jquery.com/on/
使用.onon jQuery 1.7+版本可以解决这个问题.
不幸的是,这在我的代码中不起作用(我有1.11)所以我用过:
$('body').delegate('.logout-link','click',function() {
http://api.jquery.com/delegate/
从jQuery 3.0开始,.delegate()已被弃用.自jQuery 1.7以来它被.on()方法取代,所以它的使用已经不再使用了.但是,对于早期版本,它仍然是使用事件委派的最有效方法.有关事件绑定和委派的更多信息,请参见.on()方法.通常,这些是两种方法的等效模板:
// jQuery 1.4.3+
$( elements ).delegate( selector, events, data, handler );
// jQuery 1.7+
$( elements ).on( events, selector, data, handler );
Run Code Online (Sandbox Code Playgroud)
这个评论可能会帮助别人:)!
尝试这个
从 jQuery 1.7+ 版本开始, on() 方法是 bind()、live() 和 delegate() 方法的新替代品。
所以添加这个,
$(document).on("click", "a.new_participant_form" , function() {
console.log('clicked');
});
Run Code Online (Sandbox Code Playgroud)
或欲了解更多信息,请点击此处