doupai6875
2018-01-29 12:36 阅读 363

在动态生成的php按钮上绑定事件

I have a simple table that manages a list of user created posts. Inside the table, I have two buttons that are supposed to manage the edit or the deletion of the single posts.

I have a problem with these buttons, when I try to edit one entry that isn't the first one listed, no actions occur. How i can manage this? I know that every button need an unique id, but i can't figure out how to fix this because the buttons are dynamically generated with a php for() loop.

<tbody>
    <? for($n=0;$n<count($load);$n++): ?>
    <tr>
    <th scope="row"><? echo $n; ?></th>
        <td class="postListTitle"><? echo $load[$n]['post_title']; ?></td>
        <td id="postListDate"><? echo $load[$n]['post_date']; ?></td>
        <td class="button-group"><button class="btn btn-warning btn-sm" id="btn-edit-post">Edit</button><button class="btn btn-danger btn-sm" id="btn-delete-post">Delete</button></td>
    </tr>
    <? endfor; ?>
</tbody>

Jquery code:

$('#btn-edit-post').on('click',function(e){
    e.preventDefault();
    var postTitle = $('.postListTitle').html();

    $.ajax({
        url:'system/ajax/doPost.php?title='+encodeURIComponent(postTitle),
        type:'GET',
        cache: false,
        success: function(item){
            var post = JSON.parse(item);

            $('#edit-title').attr('value',post.title);
            $('#edit-text').append(post.text);
        }
    });
});
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongqiang5541 dongqiang5541 2018-01-29 12:55

    First thing you need to understand is that all your button have the same ID which is btn-edit-post and btn-delete-post an ID attribute must be unique, therefore you need to create the ID's dynamically.Like this

    <button class="btn btn-warning btn-sm" id="edit_<?=$load[$n]['post_id']?>">Edit</button>
    <button class="btn btn-danger btn-sm" id="delete_<?=$load[$n]['post_id']?>">Delete</button></td>
    

    See the ID is dynamically for the edit make the id edit_(PoSTID) for the delete make it delete_(POSTID)

    Then on your jquery

    <script type="text/javascript">
                $('[id^="edit_"]').on('click',function(){
                var id = $(this).attr('id').split("_")[1]; //get the post ID
    
                //continue what you need.
    
            });
            </script>
    

    delete

    <script type="text/javascript">
                $('[id^="delete_"]').on('click',function(){
                var id = $(this).attr('id').split("_")[1]; //get the post ID
    
                //continue what you need.
    
            });
            </script>
    
    点赞 评论 复制链接分享
  • douyi4912 douyi4912 2018-01-29 12:53

    You need to bind the selector to a static element.

    $(document).on('click','#btn-edit-post', function(e){
        ...
    });
    

    Also I agree with the comment, if you have multiple rows that have the buttons share with the same id, you better switch to use class instead.

    点赞 评论 复制链接分享