我有新闻推送页面,在这里所有的帖子将显示我想要得到这个特定帖子的评论价值和id,进入用户评论

问题描述 投票:0回答:1

我正在开发一个社交网站,我有一个新闻源页面,所有的职位将显示我想要得到的评论此职的在其上的用户comment.i正在使用jQuery的每个功能的ID,但它也得到了从输入字段是空的,在其上用户不评值..

{{#each posts}}
<div class="form-inline" class="commentForm">
       <div class="form-group">
       <input type="hidden" name="id" value="{{id}}" class="postId">
       <input class="form-control comment" type="text" placeholder="Your 
       comments" />
     </div>
<div class="form-group">
        <input type="submit" value="Add" class="btn btn-default set send" 
         >Add</input>
  </div> 
      </div>
           </div>               
          {{/each}}

            $(".send").click(function(){


                $('input[type="text"].comment').each(function () {

             comment = $(this).val();
($(this).val(''));


});


  $('input[type="hidden"].postId').each(function () {

    comment = $(this).val();

}

的console.log(postidn);

 });

});

我想通过点击发送按钮,用户评论,要获得该ID和特定岗位的评论..

jquery node.js handlebars.js
1个回答
0
投票

无需环路..你可以只使用.closest().find()

$(".send").click(function(e){
   e.preventDefault(); // prevent default form redirect
   var ThisForm = $(this).closest('.commentForm');   // get this parent form
   var Id = ThisForm.find('input.postId').val();     // get postId
   var Comment = ThisForm.find('input[type="text"].comment').val();  // get comment
   console.log(Id +' ||| '+ Comment);
});

活生生的例子

$(".send").click(function(e){
   e.preventDefault(); // prevent default form redirect
   var ThisForm = $(this).closest('.commentForm');   // get this parent form
   var Id = ThisForm.find('input.postId').val();     // get postId
   var Comment = ThisForm.find('input[type="text"].comment').val();  // get comment
   console.log(Id +' ||| '+ Comment);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-inline commentForm">
  <div class="form-group">
    <input type="hidden" name="id" value="{{id}}" class="postId">
    <input class="form-control comment" type="text" placeholder="Your 
    comments" />
  </div>
  <div class="form-group">
    <input type="submit" value="Add" class="btn btn-default set send" value="Add">
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.