首页 > 解决方案 > 我如何从表的每一行中获取值并将它们存储在 js 数组中

问题描述

我有这段代码可以生成一个 html 表,我想在单击按钮时从行中获取所有数据并将其存储在 js 数组中。

    <table cellpadding="0" cellspacing="0" width="25%" style="margin-top: 3em;" align="center">
<tr align="center">
    <td>&nbsp;</td>
    <td colspan="2" style="font-size:small;">Anualitat seleccionable</td>   
</tr>

<tr align="center">
    <td class="tau4">&nbsp;</td>
    <td class="tau4">Empleat</td>
    <td class="tau4">Gestor</td>    
</tr>
<logic:iterate id="i" name="anys_irpf" indexId="count">
    <tr align="center" class="dadesAnys">
        <td class="tau2" width="30%">${i.any}</td>
        <td class="tau2" width="35%">
            <c:choose>
                <c:when test="${i.anyEmpleat == 1}">
                    <input type="checkbox" id="cbox1" value="cbox1" checked>
                </c:when>
                <c:otherwise>
                    <input type="checkbox" id="cbox1" value="cbox1">
                </c:otherwise>
            </c:choose>
        </td>
        <td class="tau2" width="35%">
            <c:choose>
                <c:when test="${i.anyGestor == 1}">
                    <input type="checkbox" id="cbox2" value="cbox2" checked>
                </c:when>
                <c:otherwise>
                    <input type="checkbox" id="cbox2" value="cbox2">
                </c:otherwise>
            </c:choose>
        </td>
    </tr>
</logic:iterate>
</table>

这是生成表格的代码。

标签: javascripthtmljspstruts

解决方案


为该行的所有 td 标签提供相同的类名,因此当您单击按钮时,将获取类名的所有元素,并在 for 循环中添加该元素的所有值


推荐阅读