Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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 如何获得foreach循环中每个元素的jQuery函数?_Javascript_Jquery - Fatal编程技术网

Javascript 如何获得foreach循环中每个元素的jQuery函数?

Javascript 如何获得foreach循环中每个元素的jQuery函数?,javascript,jquery,Javascript,Jquery,我有一个foreach循环显示帖子,所有帖子都有一个评论按钮 <button type="button" class="commentbtn" id="comment"> <i class="em em-thought_balloon"></i> </button> 有什么办法可以轻松解决这个问题吗 编辑:这是我使用的HTML,它是Laravel Collective {!! Form::open(['method'=>'POST

我有一个foreach循环显示帖子,所有帖子都有一个评论按钮

<button type="button" class="commentbtn" id="comment">
    <i class="em em-thought_balloon"></i>
</button>
有什么办法可以轻松解决这个问题吗

编辑:这是我使用的HTML,它是Laravel Collective

 {!! Form::open(['method'=>'POST','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() !!}
这将在按下任何按钮时起作用

更新:我将
#commentform
id选择器更改为
.commentform

如果您的
.commentform
位于按钮下方,则此功能将起作用


更新:

我不知道laravel,但我猜您提供的表单的结果html是:

  <form method="POST" class="commentForm">
     <textarea name="Comment" cols="50" rows="2"></textarea>
     <button name="Comment" type="submit" class="btn btn-primary">Comment</button>
     <button name="Cancel" class="btn btn-default cancelComment">Cancel</button>
  </form>
以及jquery版本:

  $(document).ready(function(){
     $('.commentForm').each(function(index){
        $(this).hide(); // Hide all the forms
     });
     $('.commentbtn').each(function(index){
        // Show the corresponding form
        $(this).parend().find('.commentForm').show();
     });
  });
仅当您使用如下容器包装每个表单按钮html时,此操作才有效:

  <div>
     <form method="POST" class="commentForm">
        <textarea name="Comment" cols="50" rows="2"></textarea>
        <button name="Comment" type="submit" class="btn btn-primary">Comment</button>
        <button name="Cancel" class="btn btn-default cancelComment">Cancel</button>
     </form>
     <button type="button" class="commentbtn" id="comment">
        <i class="em em-thought_balloon"></i>
     </button>
  </div>

评论
取消
这将在按下任何按钮时起作用

更新:我将
#commentform
id选择器更改为
.commentform

如果您的
.commentform
位于按钮下方,则此功能将起作用


更新:

我不知道laravel,但我猜您提供的表单的结果html是:

  <form method="POST" class="commentForm">
     <textarea name="Comment" cols="50" rows="2"></textarea>
     <button name="Comment" type="submit" class="btn btn-primary">Comment</button>
     <button name="Cancel" class="btn btn-default cancelComment">Cancel</button>
  </form>
以及jquery版本:

  $(document).ready(function(){
     $('.commentForm').each(function(index){
        $(this).hide(); // Hide all the forms
     });
     $('.commentbtn').each(function(index){
        // Show the corresponding form
        $(this).parend().find('.commentForm').show();
     });
  });
仅当您使用如下容器包装每个表单按钮html时,此操作才有效:

  <div>
     <form method="POST" class="commentForm">
        <textarea name="Comment" cols="50" rows="2"></textarea>
        <button name="Comment" type="submit" class="btn btn-primary">Comment</button>
        <button name="Cancel" class="btn btn-default cancelComment">Cancel</button>
     </form>
     <button type="button" class="commentbtn" id="comment">
        <i class="em em-thought_balloon"></i>
     </button>
  </div>

评论
取消

您正在使用相同的ID多路径时间通过循环进行绑定。不要这样做。使用类

$(".commentform").hide();
    $(".commentbtn").click(function(event) {
       $(this).next().find('.commentform').show();
  });

您正在使用相同的ID多路径时间通过循环绑定。不要这样做。使用类

$(".commentform").hide();
    $(".commentbtn").click(function(event) {
       $(this).next().find('.commentform').show();
  });

document
中元素的
id
应该是唯一的id应该是唯一的,可以使用类来附加事件,或者如果不能使id唯一并希望使用它附加事件,则可以使用
$(“[id='comment']”)
Where and what is
#commentform
?需要更多的HTML
id
文档中元素的
应该是唯一的,id应该是唯一的,或者使用类来附加事件,或者如果您不能使id唯一并想使用它来附加事件,那么可以使用
$(“[id='comment']”
Where and what is
#commentform
?需要更多的HTMLEAH,但在他提供的代码中,他没有显示任何类
。commentbtn
,这就是为什么我在我的answer@MerunasGrincalaitis然后在HTML中添加一个类。不要使用
id
来表示多个元素。这就是为什么要这样做的全部原因是的,但在他提供的代码中,他没有显示任何类
。commentbtn
,这就是为什么我在我的answer@MerunasGrincalaitis然后在HTML中添加一个类。不要使用
id
来表示多个元素。这就是这个错误的全部原因。它可能是浏览r相关。是的,当我点击任何按钮时,它会像我想要的那样打开表单。但是,出于某种原因,它只在最近的帖子中显示我的表单。当我在以后的帖子中点击评论时,表单只在最近的帖子中打开。我更新了代码。为每个表单添加一个类
commentform
。顺便问一下,您能告诉我们如何操作吗你是否用html编写了你的评论表单?这会很有帮助,因为我需要知道表单的位置。我已经更新了上面的答案,看看它,让我知道它是如何运行的。是的,当我单击任何按钮时,它会像我所希望的那样打开表单。但是,出于某种原因,它只会在最近的帖子中显示我的表单。当我单击在以后的帖子评论中,表单只在最近的帖子中打开。我更新了代码。为每个表单添加一个类
commentform
。顺便问一下,你能告诉我们你是如何用html编写你的commentform的吗?这会很有帮助,因为我需要知道表单的位置。我已经更新了上面的答案,请查看并让我知道进展如何。