如何针对foreach循环元素定位JQuery中的特定按钮

时间:2017-05-26 01:29:49

标签: php jquery laravel foreach

当我点击这个"评论" foreach循环中帖子的按钮

<button type="button" class="commentBtn">
<i class="em em-thought_balloon"></i>
</button>

我试图让这张表格打开

{!! Form::open(['method'=>'POST', 'style'=>'display: none;', 'class'=>'commentform']) !!}
{!! Form::textarea('Comment', null, ['size'=>'50x2']) !!}
{!! Form::button('Comment', ['type'=>'submit', 'class'=>'btn btn-primary']) !!}
{!! Form::button('Cancel', ['class'=>'btn btn-default cancelcomment']) !!}
{!! Form::close() !!}

我正在使用Laravel,按钮位于刀片模板的foreach循环中。表单本身也在foreach循环中。当我单击按钮时,我的jQuery函数正在运行,但它为我的foreach循环中的每个帖子打开表单,而不仅仅是目标帖子。这是功能:

$(document).ready(function(){
   $(".commentform").hide();
       $(".commentBtn").click(function(e) {
          $(".commentform").show();
    });
});

如何修复此功能,以便只有帖子的目标按钮才能打开表单?而不是在foreach循环中显示的每个帖子中打开表单。

2 个答案:

答案 0 :(得分:0)

假设.commentform.commentBtn

的兄弟姐妹
$(document).ready(function(){
   $(".commentform").hide();
       $(".commentBtn").click(function(e) {
          $(this).siblings(".commentform").show();
    });
});

答案 1 :(得分:0)

您可以在点击按钮后直接获得评论表

$(document).ready(function(){
   $(".commentform").hide();
   $(".commentBtn").click(function(e) {
      $(this).next(".commentform").show();
   });
});