douyan2970 2017-06-02 07:03
浏览 69
已采纳

如何从laravel刀片循环中获取jQuery的变量类名

I want to Show/Hide replies for each comment. In order to do that, my jQuery selector has to be variable class name (so that I can show/hide replies of a specific comment without affecting replies of other comments). I've written the PHP code appending comment_id with the class to make the classes different. But I get these IDs from Laravel blade loop and I do not know how to do the same in jQuery. Here's my blade.php -

@foreach($comments as $comment)
  <div class="box-comment">
    <div class="comment-text">
      {{$comment->body}}<br />
      <button class="btn btn-link text-muted toggle-replies-{{$comment->id}}">Show/Hide Replies</button>
    </div> <!-- /.comment-text -->
  </div> <!-- /.box-comment -->
  <div class="box-comment reply-box-{{$comment->id}} initially-hidden" style="padding-left: 30px;">
    <input type="text" placeholder="Write a reply...">
  </div>
  @foreach($comment->replies as $reply)
    @if($comment->id == $reply->comment_id)
      <div class="box-comment reply-box-{{$comment->id}} initially-hidden" style="padding-left: 30px;">
        <div class="comment-text">
          {{$reply->body}}
        </div> <!-- /.comment-text -->
      </div> <!-- /.box-comment -->
    @endif
  @endforeach                      
@endforeach            

And this is jQuery:

<script>
$(document).ready(function() {
  $(".toggle-comments").click(function(){
    $(".comment-box").slideToggle();
  });

  $(".toggle-replies-1").click(function(){
    $(".reply-box-1").slideToggle();
  });
});  
</script>

I've manually put a '1' there and it shows/hides the replies of the first comment. I need to do it for all the comments. It shouldn't be difficult but I'm still learning jQuery. I hope someone can help me with it.

  • 写回答

1条回答 默认 最新

  • dsjq6977 2017-06-02 07:18
    关注

    Try this :

    @foreach($comments as $comment)
        <div class="box-comment">
            <div class="comment-text">
                {{$comment->body}}<br />
                <button class="btn btn-link text-muted show_hide_replies" id="{{$comment->id}}">Show Replies</button>
            </div>
        </div>
        <div class="box-comment reply-box-{{$comment->id}} initially-hidden" style="padding-left: 30px;">
            <input type="text" placeholder="Write a reply...">
        </div>
        @foreach($comment->replies as $reply)
            @if($comment->id == $reply->comment_id)
                <div class="box-comment reply-box-{{$comment->id}} initially-hidden" style="padding-left: 30px;">
                    <div class="comment-text">
                        {{$reply->body}}
                    </div>
                </div>
            @endif
        @endforeach                      
    @endforeach            
    
    <script type="text/javascript">
    $(document).ready(function() {
        $(".toggle-comments").click(function(){
            $(".comment-box").slideToggle();
        });
    
        $(".show_hide_replies").click(function(ev) {
            var getElem = $(this).attr("id");
            $(".reply-box-"+getElem).slideToggle()
            $(this).text(function(i, text){
                return text === "Show Replies" ? "Hide Replies" : "Show Replies";
            })
        })
    });  
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补