附加按钮 onclick 不会触发 Jquery [英] appended button onclick don't fire Jquery

查看:23
本文介绍了附加按钮 onclick 不会触发 Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,它在表 td 中附加 2 个文本字段和一个按钮,但是问题是附加按钮的 onclick 功能不会触发.有人看到问题了吗?代码:

I am having a button which appends 2 textfields and a button in a table td, however the problem is that the onclick function of the appended button don't fire. anyone sees the problem? code:

<script>
    $(document).ready(function(){
        $("#add").click(function() {
            $(".no_border").append("<br /><label>Co-author email:</label><input type='text' name='author_email'/><label>Co-author Level:</label><input type='text' name='author_level'/><input id='save' type='button'name='save' value='Add'/>");
    });
});
</script>
<?php
    while($row = mysql_fetch_assoc($co_authors)) {
        echo "<tr>
            <td>{$row['author_email']}</td>
            <td>{$row['coauthor_level']}</td>";
            ?><td><button class='remove' id='remove' name='remove' email="<?php echo $row['author_email'] ?>"
            paper="<?php echo $row['paper_id'] ?>">Remove</button></td>
            </tr><?
            ?><td colspan="3" class="no_border"><button class="add" name="add"></button>
            <label id="add" class="cursor_pointer"> Add more co-authors</label></td><?
    }
?>

推荐答案

您将需要使用 jQuery .on() 功能来访问在 DOM 加载后创建的元素.

You will need to use the jQuery .on() functionality to access elements created after the DOM has loaded.

更多具体信息可以在此处的 jQuery 手册中找到:http://api.jquery.com/on/

More specific info can be found in the jQuery manual here: http://api.jquery.com/on/

$( document ).on( "click", "#add", function() {
    alert("aaaaaaaaaaa");
} ); 

在 jQuery 中1.7 ,这以前是这样使用的:

In jQuery < 1.7 , this was previously used like so:

$( "#add" ).live( "click", function( e ) {} );

这篇关于附加按钮 onclick 不会触发 Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆