Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails在索引页面中的单个帖子上使用jquery调用评论框,其中列出了所有帖子_Javascript_Jquery_Ruby On Rails_Facebook_Comments - Fatal编程技术网

Javascript Rails在索引页面中的单个帖子上使用jquery调用评论框,其中列出了所有帖子

Javascript Rails在索引页面中的单个帖子上使用jquery调用评论框,其中列出了所有帖子,javascript,jquery,ruby-on-rails,facebook,comments,Javascript,Jquery,Ruby On Rails,Facebook,Comments,我的索引文件中有一个帖子列表。在每个帖子上我都想发表评论。我在每个帖子上都有一个名为“显示评论”的按钮。当我按ShowComment时,我希望jquery触发特定帖子上的注释框 现在的问题是,我触发了整个页面上的所有显示评论 这是我的密码: 我认为问题在于剧本 <% @posts.each do |post| %> <div class="span6"> <h4><%= post.title %></h4> <p&

我的索引文件中有一个帖子列表。在每个帖子上我都想发表评论。我在每个帖子上都有一个名为“显示评论”的按钮。当我按ShowComment时,我希望jquery触发特定帖子上的注释框

现在的问题是,我触发了整个页面上的所有显示评论

这是我的密码:

我认为问题在于剧本

    <% @posts.each do |post| %>
<div class="span6">
  <h4><%= post.title %></h4>
  <p><%= post.text %></p>
   <p><%= link_to "Go to", post_path(post) %></p>

<button class="btn btn-large btn-block btn-primary comments-show-button"  type="button">Show comments</button>
        <button id="remove-button" class="btn btn-large btn-block btn-primary" type="button">Remove comments</button>
    <div class="comments">
        <div class="fb-comments" data-href="http://example.com" data-width="430"></div>
    </div>  
</div>
<% end %>
    </div>
</div>

显示评论 删除评论


$(文档).ready(函数(){
$(“.comments show button”)。单击(函数(){
$(“.comments”).show(500);
$(“#删除按钮”)。显示(500);
$(“.comments show button”).hide();
});
$(“.comments”).mouseout(函数(){
$(“#注释显示按钮”).hide();
});
});

谢谢你的帮助

太棒了,谢谢你的帮助。我以后会在我的应用程序中经常用到这个:)接下来我要做一个函数。当我点击删除按钮时,注释和显示按钮返回。仍然只在一个单一的职位。但是你不能让它工作。这是因为您的
#remove_按钮使用的是ID而不是类,所以只有在第一次发布时才有效。每个页面只允许有一个唯一的ID。把它改成一个类,它就会起作用。
$(document).ready(function(){
    $(".comments-show-button").click(function() {
        var $thisPost = $(this).closest('div.span6');
        var $comments = $thisPost.find('div.comments');
        $comments.show(500);
        $thisPost.find("#remove-button").show(500);
        $thisPost.find(".comments-show-button").hide();
    });
    $(".comments").mouseout(function(){
        $("#comments-show-button").hide();
    });
});
$(document).ready(function(){
    $(".comments-show-button").click(function() {
        var $thisPost = $(this).closest('div.span6');
        var $comments = $thisPost.find('div.comments');
        $comments.show(500);
        $thisPost.find("#remove-button").show(500);
        $thisPost.find(".comments-show-button").hide();
    });
    $(".comments").mouseout(function(){
        $("#comments-show-button").hide();
    });
});