JSP Javascript从foreach JSTL中的隐藏输入中获取id

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

我在这里遇到一个问题:我想在javascript文件的隐藏输入中获取commentUserId的ID以放入ajax。但我得到的只是第一排的价值。

这是设计代码

<c:forEach items="${commentList}" var="items">
<div class="media" style="padding: 10px 0">
    <div class="media-body">               
        <input type="hidden" id="commentUserId" name="commentUserId" value="${items.accountId.accountId}"/>                                                                     
            <a id="${items.commentId}" name="btnReportComment" class="btn btn-report">\Report</a>    
    </div> 
</div> 

这是javascript中的代码

$('a[name=btnReportComment]').click(function() {
var commentUserId = $('#commentUserId').val();
alert(commentUserId);})
javascript jquery jsp jstl
1个回答
0
投票

尝试更新您的点击事件,例如:

$('a[name=btnReportComment]').click(function() {
  var commentUserId = $(this).closest('.media-body').find('[name="commentUserId"]').val();
  alert(commentUserId);
})
  • 在这里,我们使用this来获取点击btnReportComment的参考。
  • 然后我们使用commentUserIdclosest找到find输入w.r.t。
© www.soinside.com 2019 - 2024. All rights reserved.