简体   繁体   English

我如何从表的每一行获取值并将其存储在js数组中

[英]How do i get the values from each row of a table and store them in a js array

I have this code that generates an html table and i want to get all the data from the rows when a button is clicked and store it in a js array. 我有这段代码生成一个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>

this is the code that generates the table. 这是生成表的代码。

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

var myTab = document.getElementById('tableID');
        var tableData = [];
    // LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.
    for (i = 1; i < myTab.rows.length; i++) {

        // GET THE CELLS COLLECTION OF THE CURRENT ROW.
        var objCells = myTab.rows.item(i).cells;

        // LOOP THROUGH EACH CELL OF THE CURENT ROW TO READ CELL VALUES.
        for (var j = 0; j < objCells.length; j++) {
            tableData.push(objCells.item(j).innerHTML);
            }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Js如何将值数组存储在各自键中的每一行中 - Firebase Js How do store an array of values into each row in their respective key 如何以 JSON 格式从多个 object 中获取值并将它们存储在数组中? - How Do I grab values from multiple object in a JSON format and store them in an array? 如何从MVC项目的每个表行中获取每个值? - How do I get each value from each table row in an MVC project? 如何在Promise JS中将Promise数组转换为每个Promise解析的值的数组? - How do I convert an Array of Promise to an Array of the values resolved from each Promise in vanilla JS? 如何多次从数据列表下拉列表中选择值,然后每次将值存储在表的新行中? - How can i select values from datalist dropdown multiple times and store the value each time in a new row in table? 如何在Javascript中获取表中每一行的值 - How to get the values of each row in a table in Javascript 如何检查一个数组中的值是否在单独的数组中,然后将其转换为新值后返回这些值 - How do I check if values in one array feature in a separate array, and then if they do, return those values after converting them each to new values 如何将标记为“价格”的所有 class 值存储到一个数组中以便对它们进行排序? - How do I store all my class values labeled "price" into an array so I can sort them? 使用JavaScript从html文档中的链接获取值并将它们存储在数组中 - get values from links in html document with JavaScript and store them in an array 如何从数组中获取值? - How do I get values from an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM