【问题标题】:Comment delete not working评论删除不起作用
【发布时间】:2013-11-16 07:48:10
【问题描述】:

我正在尝试使用带有 Datamapper 的 Jquery AJAX 和 PHP(codeigniter) 删除论坛评论 但由于某种原因,无论我做什么,当我点击删除时都没有任何反应。

如果有人能看到问题,那就太好了。

jquery =

$('button.delete').on('click', function(event) {

    console.log('derp');
    // var comment = $(this).parent('.comment').attr('id');

    // $.post('actions/delete_comment/'+comment);

});

论坛帖子 php =

<li class="comment" id="<?=$comment->id ?>">
    <img  src="assets/img/avatars/<?=$comment->user.'.jpg' ?>">
    <p class="username"><?=$comment->user ?></p><p class="commenttime"><?=$comment->date ?></p><br>
    <p><?=$comment->contents ?></p>
    <button class="delete">Delete</button>
</li>

删除 php =

public function delete_comment($id){
    $comments = new Forum_comment;
    $comments->get_where(array('id' => $id));
    $comments->delete();
}

【问题讨论】:

  • 你的 .comment 类元素没有 id 属性

标签: jquery ajax forum


【解决方案1】:

在你的 jquery 中试试这个

$(document).on('click', 'button.delete', function(event) {

    console.log('derp');
    // var comment = $(this).parent('.comment').attr('id');

    // $.post('actions/delete_comment/'+comment);

});

当然,当代码开始进入事件处理程序时,请取消注释。

【讨论】:

    【解决方案2】:

    您注释掉了 jQuery 事件中的所有实际代码,除了 console.log()

    $('button.delete').on('click', function(event) {
    
        console.log('derp');
        var comment = $(this).parent('.comment').attr('id');  // Remove leading slashes
    
        $.post('actions/delete_comment/'+comment);  // Remove leading slashes
    
    });
    

    当然什么也没发生!

    【讨论】:

      【解决方案3】:

      您需要确保在调用 jQuery 时使用 $('button.delete) 选择的元素确实存在。将 Javascript 移动到 &lt;body&gt; 的底部,或者将 javascript 包装在 domReady 函数中:

      $(document).ready(function() {

      $(button.ready)...// do your initialisation here.
      

      });

      【讨论】:

        【解决方案4】:

        除了注释掉执行你想要它做的实际代码之外,你还没有为类'comment'的li标签定义一个id属性。

        所以在这种情况下,var comment = $(this).parent('.comment').attr('id'); 不会返回任何内容。

        li 标签必须是&lt;li class="comment" id=(insert_comment_id_here)&gt;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-10
          • 1970-01-01
          • 1970-01-01
          • 2021-05-01
          • 1970-01-01
          • 2011-12-18
          • 2017-10-24
          相关资源
          最近更新 更多