相关疑难解决方法(0)

如何使用jQuery将HTML表转换为Javascript对象

我正在尝试转换此HTML表格:

在此输入图像描述

码:

<table id="students" border="1">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Grade</th>
        </tr>
    </thead>
    <tbody>
        <tr class="student">
            <td>Oscar</td>
            <td>23</td>
            <td>16.5</td>        
        </tr>
        <tr class="student">
            <td>Antonio</td>
            <td>32</td>
            <td>14</td>        
        </tr>
        <tr class="student">
            <td>Jessica</td>
            <td>21</td>
            <td>19</td>        
        </tr>
    </tbody>
</table>??????
Run Code Online (Sandbox Code Playgroud)

使用jQuery进入javascript对象:

var tbl = $('table#students tr').map(function() {
  return $(this).find('td').map(function() {
    return $(this).text();
  }).get();
}).get();
Run Code Online (Sandbox Code Playgroud)

上面的代码将输出以下数组:

["Oscar", "23", "16.5", "Antonio", "32", "14", "Jessica", "21", "19"]
Run Code Online (Sandbox Code Playgroud)

此时一切都很好但如果我希望数组中的javascript对象具有以下结构,我该怎么办:

[{id:1, name: "Oscar", age: 23, grade: 16.5}, {id:2, name: "Antonio", age: 32, grade: 14}, {id:3, name: "Jessica", age: 21, grade: 19}]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

只是为了更具体.​​..... …

javascript jquery

23
推荐指数
3
解决办法
3万
查看次数

将Html表转换为JSON

我已经创建了一个示例应用程序,它将html表转换为JSON.问题是JSON没有重复值,我想从JSON中删除最后两列.

我生成的JSON如下所示

[
   {
      "Person Name":"Smith",
      "Score":"disqualified",
      "Price":"150",
      "Tax":"41"
   },
   {
      "Person Name":"Jackson",
      "Score":"94",
      "Price":"250",
      "Tax":"81"
   },
   {
      "Person Name":"Doe",
      "Score":"80",
      "Price":"950",
      "Tax":"412"
   },
   {
      "Person Name":"Johnson",
      "Score":"67",
      "Price":"750",
      "Tax":"941"
   }
]
Run Code Online (Sandbox Code Playgroud)

但我期望的JSON就像

[
   {
      "Person Name":"Jill",
      "Person Name":"Smith",
      "Score":"disqualified"
   },
   {
      "Person Name":"Eve",
      "Person Name":"Smith",
      "Score":"94"
   },
   {
      "Person Name":"John",
      "Person Name":"Smith",
      "Score":"80"
   },
   {
      "Person Name":"Adam",
      "Person Name":"Smith",
      "Score":"67"
   }
]
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我如何从表中生成上述JSON

我的代码如下所示.

HTML代码

<table id='example-table'>
    <thead>
    <tr>
        <th>Person Name</th>
        <th>Person Name</th>
        <th data-override="Score">Points</th>
        <th>Price</th>
        <th>Tax</th>
    </tr>
    </thead> …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json

7
推荐指数
1
解决办法
3万
查看次数

使用jQuery迭代HTML表,将表中的数据转换为JSON

我遇到了一个必须将HTML表格数据转换为JSON的情况.在这个过程中,我必须遍历表并逐个(行)转换为数组,然后将整个数组转换为JSON.如何遍历表(每行和每列)?

html jquery json html-table

3
推荐指数
1
解决办法
1万
查看次数

将jquery datatable数据转换为json

我正在使用jquery数据表。我有一张桌子如下

<table id="employees">
  <thead>
     <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
      </tr>
    </thead>
   <tbody>
      <tr>
           <td>1</td>
           <td>Karthik</td>
           <td>Kk@gmail.com</td>
           <td>1234</td>
       </tr>
       <tr>
           <td>1</td>
           <td>Karthik</td>
           <td>Kk@gmail.com</td>
           <td>1234</td>
       </tr>
     </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

我将表转换为jquery datatable作为 $('#employees').DataTable()

我想将我的jquery数据表转换为json格式。请帮助我将其转换为

[{"Id":"1", "Name":"Karthik","Email":"kk@gmail.com","Phone":"1234"}]
Run Code Online (Sandbox Code Playgroud)

jquery json datatables

3
推荐指数
3
解决办法
1万
查看次数

将表格 HTML 转换为 JSON

我有这个:

<table>
    <tr>
        <th>Name:</th>
        <td>Carlos</td>
    </tr>        
    <tr>
        <th>Age:</th>
        <td>22</td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我需要一个 JSON 格式。

{"Name":"Carlos","Age": 22}
Run Code Online (Sandbox Code Playgroud)

我试过https://github.com/lightswitch05/table-to-json但它不适用于每一行的标题:(

编辑:http : //jsfiddle.net/Crw2C/773/

html javascript json html-table

2
推荐指数
2
解决办法
1万
查看次数

使用jquery从html表中收集数据

我需要从HTML表中收集数据并将其发送到服务器.我必须使用JQuery.有我的桌子

    <table id="table" border=1>
    <thead> <tr>
    <th>First</th>
    <th>Last</th>
    <th>Date of birth</th>
    <th>City</th>
    </tr></thead>
    <tbody>
    <tr>
    <td>TEXT1</td>
    <td>TEXT2</td>
    <td>TEXT3</td>
    <td>TEXT4</td>
    </tr>
    <tr>
    <td>TEXT5</td>
    <td>TEXT6</td>
    <td>TEXT7</td>
    <td>TEXT8</td>
     </tr>
    <tr>
    <td>TEXT9</td>
    <td>TEXT10</td>
    <td>TEXT11</td>
    <td>TEXT12</td>
   </tr>
   </tbody>
   </table>
Run Code Online (Sandbox Code Playgroud)

html jquery html-table collect

0
推荐指数
1
解决办法
9642
查看次数

标签 统计

jquery ×5

json ×4

html ×3

html-table ×3

javascript ×3

collect ×1

datatables ×1