【问题标题】:jquery how to add comments to a postjquery如何在帖子中添加评论
【发布时间】:2013-07-01 06:35:16
【问题描述】:

您好,我目前正在创建一个“论坛”,用户可以在其中提交帖子并显示。我想要添加评论的选项。

我在客户端做所有事情。

这是生成论坛的代码:

$.get(url, page, function(data){
      $("#forum").html("");
      for(var i in data)
      {
        $("#forum").append(buildForumEntry(data[i]));
      }
    });

// Builds the forum entry
  function buildForumEntry(post)
  {
    var titleAndType = '<div><span class="forum-title">'+post.title+'</span>'+buildType(post.type)+'</div>';
    var author =  "<div class=\"forum-author\">By: "+post.author+" on "+formatDate(post.date)+"</div>";
    var body =    "<pre>"+post.body+"</pre>";
    var comment = "<div class=\"forum-comment\"> <div class=\"btn-group\"><a class=\"btn btn-mini btn-primary\" role=\"button\" data-toggle=\"modal\" id=\"btn-forum-comment\"><i class=\"icon-comment icon-white\"></i> comment</a></div></div>";
    var footer =  "<hr style=\"border-top: 1px dotted #b0b0b0;border-bottom: 0px\">";
    var entry = titleAndType+author+body+comment+footer;
    return entry;
  }

我怎么能这样做,当我点击评论时,我会获取帖子的一些信息?也在这个方法中调用:

// Button comment
  $("#btn-forum-comment").click(function() {
    alert("clicked");
  });

这不起作用,因为“id=btn-forum-comment”的类型超过 1 种。我应该使用类吗?如果是这样,我如何获取更多实例帖子的标题,以便当我发布到我的服务器时我可以更新正确的条目。

更新:类在生成的论坛中不起作用。按钮点击没有被触发。有什么想法吗?

var comment = '<div class="btn-group"><a class="btn btn-mini btn-primary btn-forum-comment" id="btn-forum-comment-'+post._id+'"><i class="icon-comment icon-white"></i> comment</a></div>';

// Button comment
  $(".btn-forum-comment").click(function() {
    // now you know the id of clicked comment, so you can rewrite this with code which will open a form to answer exactly this comment
    console.log("clicked");
    alert($(this).attr('id')+" clicked"); 
  });

我在点击时遇到问题,我认为它与动态加载有关。我将该类应用于硬编码的 html 并且它可以工作。但不是动态的感谢。

UPDATE2:为未来用户更新。我需要使用委托功能

$("#forum").delegate(".btn-forum-comment", "click", function() {
    console.log("clicked");
    alert($(this).attr('id')+" clicked");     
  });

【问题讨论】:

  • 一定要使用一个类,ID是唯一的!
  • 我从来没有使用过课程,我该怎么做呢?并从点击的帖子中获取信息?
  • 如果你的 id 不是唯一的,你会遇到很大的问题,你应该确保所有的 id 在你的 html 中都是唯一的。

标签: javascript jquery


【解决方案1】:

为您的按钮添加一个类:

<a class=\"btn btn-mini btn-primary btn-forum-comment\" ... id=\"btn-forum-comment-"+post.id+"\">

和一个类的帖子

  var entry = "<div class='post'>"+ titleAndType+author+body+comment+footer +  "</div>"; 

您的事件处理程序:

$(".btn-forum-comment").click(function() {
    var post = $(this).parents(".post"); //the post of this clicked button
    //from there you can access title, author,... of this post.
    var title = post.find(".forum-title");
  });

通过这种方法,您将能够访问所单击按钮的正确标题、作者……。

【讨论】:

  • 感谢您的帮助。即使其他人都提供了有效的答案。你的让我可以访问更多信息。谢谢
【解决方案2】:

你应该在btn的id中使用帖子的ID,并且你的post对象应该有这样一个字段post.id(它将包含帖子在数据库中的唯一id)

也可以使用该类来访问所有按钮

&lt;a class=\"btn btn-mini btn-primary btn-forum-comment-class\" ... id=\"btn-forum-comment-"+post.id+"\"&gt;

以及点击事件:

// Button comment
  $(".btn-forum-comment-class").click(function() {
    // now you know the id of clicked comment, so you can rewrite this with code which will open a form to answer exactly this comment
    alert($(this).attr('id')+" clicked"); 
  });

【讨论】:

    【解决方案3】:

    是的,使用一个类。

    此外,您应该将每个条目包装在容器 div 中。如果需要,这将帮助您访问论坛条目的其他部分。

    html(为简洁起见):

    <div class="forum-entry" data-id="1234">
      <div><span class="forum-title">blah blah</span>...</div>
      <pre>post body...</pre>
      <div class="forum-comment">
        <div class="btn-group">
          <a class="btn btn-mini btn-primary btn-forum-comment">comment</a>
        </div>
      </div>
      <hr />
    </div>
    

    javascript:

    $('.btn-forum-comment').click(function(ev) {
        ev.preventDefault();
        var $forumEntry = $(this).closest('.forum-entry'); // this will get you a handle to the forum entry.  from there, you can access other parts of the entry
        var forumEntryId = $forumEntry.data('id');
    
        alert('add a comment to forum entry ID ' + forumEntryId);
        // etc...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2013-12-02
      • 1970-01-01
      相关资源
      最近更新 更多