我正在开发一个社交网站,我有一个新闻源页面,所有的职位将显示我想要得到的评论此职的在其上的用户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和特定岗位的评论..
无需环路..你可以只使用.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>