【问题标题】:how to get all cell values from a table and converting in json with jquery or js如何从表中获取所有单元格值并使用 jquery 或 js 转换为 json
【发布时间】:2020-05-21 01:55:40
【问题描述】:

我有以下 HTML 代码:

<table id="resultados" class="table table-checkable dataTable no-footer ">
<tbody>
    <tr id="Rhead">
        <th style="background-color: rgb(27, 197, 189);">Código</th>
        <th style="background-color: rgb(27, 197, 189);">Proveedor</th>
        <th style="background-color: rgb(27, 197, 189);">Producto</th>
        <th style="background-color: rgb(27, 197, 189);">Precio</th>
        <th style="background-color: rgb(27, 197, 189);">Acciones</th>
        <th style="background-color: rgb(27, 197, 189);">Cantidad</th>
    </tr>
    <tr id="N1row">
        <td id="1menu">1</td>
        <td id="1menu">1</td>
        <td id="1menu">Ham Clasica</td>
        <td id="1menu">3</td>
        <td id="NCellRow1">
            <center><button id="N1button" onclick="ResponseTableButton(&quot;N1row&quot;)" class="envio btn btn-link-success"><i class="glyphicon glyphicon-trash"></i></button></center>
        </td>
        <td id="1rowCellCantidad"><input type="text" id="N1rowInputCantidad" name="N1rowInputCantidad"></td>
    </tr>
    <tr id="N3row">
        <td id="3menu">3</td>
        <td id="3menu">1</td>
        <td id="3menu">Ham Guayanesa</td>
        <td id="3menu">4.5</td>
        <td id="NCellRow3">
            <center><button id="N3button" onclick="ResponseTableButton(&quot;N3row&quot;)" class="envio btn btn-link-success"><i class="glyphicon glyphicon-trash"></i></button></center>
        </td>
        <td id="3rowCellCantidad"><input type="text" id="N3rowInputCantidad" name="N3rowInputCantidad"></td>
    </tr>
    <tr id="N4row">
        <td id="4menu">4</td>
        <td id="4menu">1</td>
        <td id="4menu">Ham New York</td>
        <td id="4menu">5.6</td>
        <td id="NCellRow4">
            <center><button id="N4button" onclick="ResponseTableButton(&quot;N4row&quot;)" class="envio btn btn-link-success"><i class="glyphicon glyphicon-trash"></i></button></center>
        </td>
        <td id="4rowCellCantidad"><input type="text" id="N4rowInputCantidad" name="N4rowInputCantidad"></td>
    </tr>
</tbody>

我需要获取每个单元格的每个值,甚至是表格​​中的输入表单值,并将其转换为格式化 json,但我找不到方法

这将是我需要的 json 格式:

[ "N1row"={ 'codigo': '1', 'Proveedor': '1', 'Producto': 'Ham Clasica', 'Precio': '3', 'Cantidad': '4', }, "N3row"={ 'codigo': '3', 'Proveedor': '1', 'Producto': 'Ham Guayanesa', 'Precio': '4.5', 'Cantidad': '5', }, "N4row"={ 'codigo': '4', 'Proveedor': '1', 'Producto': 'Ham New York', 'Precio': '5.6', 'Cantidad': '6', } ]

注意:所有行都是动态生成的,可以是N行

【问题讨论】:

    标签: javascript html jquery html-table


    【解决方案1】:

    确保将&lt;tr&gt;&lt;th&gt; 放入&lt;thead&gt; 标记而不是&lt;tbody&gt;

    1. 获取对包含标头的 TR 的引用。
    2. 循环获取标题值,将它们保存到数组中。
    3. 获取对表体tbody中TR列表的引用。
    4. 在该列表上循环
    5. 对于上述循环 (4) 中的每个成员,您将根据特定 tr 内的值启动一个新循环。
    6. 对于您在 (5) 中找到的每个成员,您都知道它的名称,因为它的名称应该与您在步骤 (2) 中找到的数组中的索引相同。

    我希望从这里可以轻松地将这些键/值填充到您在所有这些循环之前实例化的 json 对象中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 2014-04-14
      • 2018-12-13
      • 2012-05-13
      • 2011-08-02
      相关资源
      最近更新 更多