jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了追加方法后,Jquery点击事件不工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有这个按钮,在表中添加一个新行,但我的问题是,它不会听到.new_participant_form单击事件后,append方法发生后。

http://jsfiddle.net/cTEFG/

单击添加新条目,然后单击表单名称

$('#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);

    });
});

HTML

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

解决方法

使用 on
$('#registered_participants').on('click','.new_participant_form',function() {

因此,即使在绑定了事件处理程序之后添加了click,也会将该click被委派给具有类new_participant_form的#registered_pa​​rticipants中的任何元素。

大佬总结

以上是大佬教程为你收集整理的追加方法后,Jquery点击事件不工作全部内容,希望文章能够帮你解决追加方法后,Jquery点击事件不工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。