2016-04-21 77 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

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