You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript点击表格按钮如何获取对应行的reqproid数据?

Solution

Step 1: Fix the HTML (Thymeleaf)

Remove duplicate id attributes from elements in the loop (ids must be unique across the page):

<tr th:each="pendingStock,iStat:${listPenddingByID}" name="line_items">
    <td th:text="${pendingStock.reqproid}" hidden name="reqproid" class="reqproid"></td>
    <td>
        <a style="padding: 10px;" class="detailsButton"><i class="ti-pencilalt">View</i></a>
    </td>
</tr>

Step 2: Correct the JavaScript

Instead of selecting all .reqproid elements, traverse the DOM to find the one in the same row as the clicked button:

$('.table .detailsButton').on('click', function(event){
    // Get the parent row of the clicked button, then find the reqproid td inside it
    let productid = $(this).closest('tr').find('.reqproid').text().trim();
    alert(productid);
});

Why the original code failed:

  • document.querySelectorAll('.reqproid') returns a NodeList of all matching elements, not a single element. Accessing .value on this list is invalid (and even if it worked, it would grab the first element's value, not the clicked row's).
  • The td element uses th:text to set its content, so you need to retrieve its text content (.text()) instead of value (which is for input elements).
  • Duplicate id attributes break HTML validity and can cause unexpected behavior with selectors.

内容的提问来源于stack exchange,提问作者Jav JakSmoke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:22:13