2016-04-21 76 views
2

我有一个帖子的索引页面。jQuery的迭代收集与

单击给定帖子的显示评论按钮后,该帖子的评论可见。这很容易,因为我可以根据点击位置使用this,然后find。现在

//open hidden post comments and replies in post thread 
$(document).on('click', '.open-all-post-comments', function (event) { 
    var post_id = $(this).data('pid'); 
    var all_replies = $('#post_' + post_id).find('.post-comment-replies:has(.post-comment-reply)'); 
    all_replies.show(); 
    $(this).closest('.open-all-post-comments-row').hide(); 
}); 

,在页面加载,我想作出在帖子的作者是当前用户的职位编辑下拉可见。我不知道如何去通过页面上的所有帖子,检查给定的数据attr是否等于当前用户的id,然后使下拉菜单可见,如果是这样。

这是我现在的代码。我应该如何改变它以使其工作?

//checking all posts on the page and show the dropdown if user is the post author 

$(document).on("page:change", function() { 
    if ($('.post-container').length > 0) { 
    if ($('.edit-post-dropdown-button').data('postauthorid') == $('#bodycurrentuser').data('currentuserid')) { 
     $('.edit-post-dropdown-button').removeClass('hidden'); 
    }; 
    }; 
}); 

彦博部分(单篇文章的HTML)

<div class="panel panel-default post-panel" id="post_<%= post.id %>"> 
    ........ 
    <li class="dropdown edit-post-dropdown-button hidden" data-postauthorid ="<%= post.user_id%>"> 
    ...... 
    </li> 
</div> 
+0

你缺少一个'$''之前(文件)'' – callback

+1

$ .each'功能 –

+0

为u_mulder说,你应该使用'$ .each'功能通过你需要更多posts.If迭代详细的答案显示你的HTML,所以我们可以帮助你 –

回答

2

尝试通过每个迭代post.I无法测试,但这样的事情应该工作:

$('.post-panel').each(function(index) { 
     if ($(this).find('.edit-post-dropdown-button').data('postauthorid') == $('#bodycurrentuser').data('currentuserid')) { 
      $(this).find('.edit-post-dropdown-button').removeClass('hidden'); 
      } 
}); 
+0

Thx乔治,我把它拉下来。 –

1

实际上,你别甚至不需要翻动页面的所有文章,您需要选择postauthorid === currentuserid

+0

弗朗切斯科,这一个不工作,因为在同一页面上来自同一用户的更多帖子。或者我错过了什么? –

+0

好吧,那么你是对的,但很容易扩展到更多的帖子,我会为你更新 –

+0

感谢弗朗切斯科,迫不及待地看到它! –