简体   繁体   English

我怎样才能让我的jQuery函数可用于foreach循环中的每个元素?

[英]How can I get my jQuery function for work for each element inside a foreach loop?

I have a foreach loop displaying posts, all of them with a comment button 我有一个显示帖子的foreach循环,所有帖子都带有评论按钮

<button type="button" class="commentbtn" id="comment">
    <i class="em em-thought_balloon"></i>
</button>

The function itself is working fine, but only for the most recent post displayed, not the other ones. 该函数本身运行良好,但仅适用于显示的最新帖子,而不适用于其他帖子。 I'm trying to get it to work when each post comment button is clicked separately. 当每个帖子评论按钮分别单击时,我试图使其正常工作。 This is the function 这是功能

 $(document).ready(function(){
    $("#commentform").hide();
    $("#comment").click(function(event) {
        $("#commentform").show();
    });
});

Any ideas how I can fix this easily? 有什么想法可以轻松解决吗?

Edit: Here's the HTML I'm using, it's Laravel Collective 编辑:这是我使用的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").hide();
$(".commentbtn").each(function(index){
    $(this).click(function(event) {
        $(this).next().find(".commentform").show();
    });
});

This will work when any button is pressed. 当按下任何按钮时,这将起作用。

Update: I changed the #commentform id selector to .commentform . 更新:我将#commentform id选择器更改为.commentform

This will work if your .commentform is below the button. 如果您的.commentform位于按钮下方,则此方法将起作用。


UPDATE: 更新:

I don't know laravel but I guess the resulting html for the form you provided is is: 我不知道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>

And the comment button you said earlier: 您之前说的评论按钮:

  <button type="button" class="commentbtn" id="comment">
     <i class="em em-thought_balloon"></i>
  </button>

This is your solution with plain js, it hides each form and it displays each one when you click the .commentbtn button: 这是使用普通js的解决方案,它会隐藏每个表单,并在单击.commentbtn按钮时显示每个表单:

  'use strict';
  window.addEventListener('load', () => {
     document.querySelectorAll('.commentForm').forEach(form => {
        form.style.display = 'none'; // Hide all the forms
     });
     document.querySelectorAll('.commentbtn').forEach(button => {
        button.addEventListener('click', event => {
           // Show the corresponding form
             event.target.parentNode.querySelector('.commentForm').style.display = 'block';
        });
     });
  });

And the jquery version: 和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();
     });
  });

This will ONLY work if you wrap each form - button html with a container like this: 仅当您使用如下容器包装每个表单-按钮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>

You are using same ID multipal time to bind via loop. 您正在使用相同的ID multipal时间通过循环绑定。 Dont Do this. 不要这样做。 Use class. 使用类。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM