javascript - JQuery - 添加超过 1 条评论不起作用

标签 javascript jquery events

通过这段代码,我可以在帖子中添加评论。但是有一个问题 - 第一个评论有效,但我无法添加下一个。我使用 livequery,所以它可以工作。你能帮帮我吗?

$('form.comment_form').livequery('submit', function a()  
  {
      var element = $(this);        
      var wall_post_id = element.attr("id");
       var wall_message = $('#comment_text'+wall_post_id).attr('value');

        $.post("home/add_comment", $(this).serialize(), function(response)
        {
            if(response.error == "0")
            {
          $("div#comments"+wall_post_id).replaceWith(response.message);
            }
            else if(response.error == "1")
            {
               alert(response.message);
               return false;
            }
      }, "json");
      return false;
   });

最佳答案

问题是当您使用 replaceWith 时,它会完全删除该 DOM 元素。因此,它第二次针对第二条评论运行时,不再有匹配的 DIV,因此无需替换任何内容。尝试附加。见以下代码:

$('form.comment_form').livequery('submit', function a()  
{
  var element = $(this);        
  var wall_post_id = element.attr("id");
  var wall_message = $('#comment_text'+wall_post_id).attr('value');

    $.post("home/add_comment", $(this).serialize(), function(response)
    {
        if(response.error == "0")
        {
         $("div#comments"+wall_post_id).append(response.message);
        }
        else if(response.error == "1")
        {
           alert(response.message);
           return false;
        }
  }, "json");
  return false;
});

关于javascript - JQuery - 添加超过 1 条评论不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2389941/

相关文章:

javascript - Chrome Kiosk 模式下的安全网站

android - 每当互联网连接不可用时,如何通知我的 android 应用程序?

delphi - 如何比较一个事件及其相应的过程,避免E2035和E2036?

javascript - jquery 检查 img 是否在 div 中

javascript - 没有 jQuery 的委托(delegate)事件处理程序

javascript - JS数组拼接由于某种原因没有删除索引

php - 删除行wordpress自定义数据库表

javascript - jQuery AJAX 类型 jsonp,返回格式错误的 JSON?

javascript - IE png 修复使用相对路径时不从静态域调用的问题

php - 在谷歌日历中插入事件