appended button onclick don't fire Jquery

ぃ、小莉子 提交于 2019-12-11 09:24:34

问题


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

回答1:


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

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

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

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

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



回答2:


Probably you have to use .on or .live(), they make the script recognize elements created after $(document).ready() be called.

For example:

before jQuery 1.7

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

jQuery 1.7 and higher

$("#add").on("click", function() {
    ...



回答3:


Wrap your code in the jQuery document ready function:

$(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'/>");
   });
});

For more details see: http://api.jquery.com/ready/

Alternatively you could use .on or .live (.on being the preferred method from 1.7).

   $(document).on('click', '#add', 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'/>");
   });

   $("#add").live('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'/>");
   });


来源:https://stackoverflow.com/questions/11463109/appended-button-onclick-dont-fire-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!