【问题标题】:Getting HTML table values. Sending them with Ajax获取 HTML 表格值。用 Ajax 发送它们
【发布时间】:2018-01-16 07:25:18
【问题描述】:

我需要获取所有表值并将它们发送到我的控制器进行处理!

这是我的桌子:

<table id="test">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>6</td>
    <td>7</td>
  </tr>
</table>

如何将这 6 个值放入数组并使用 ajax 将它们发送到我的脚本进行处理?

编辑:类似于使用数据通过 ajax 提交表单时:

serialize("#form")

【问题讨论】:

标签: jquery html ajax html-table


【解决方案1】:
var values = $('#test td')  // Find all <td> elements inside of an element with id "test".
    .map(function(i, e){    // Transform all found elements to a list of jQuery objects...
        return e.innerText; // ... using the element's innerText property as the value.
    })
    .get();                 // In the end, unwrap the list of jQuery objects into a simple array.

Working fiddle here.


ES6 让这个看起来更优雅一点:

let values = $('#test td')
    .map((index, element) => element.innerText)
    .get();

【讨论】:

  • @vzwick:如果您能解释一下,那就太好了。谢谢。漂亮的答案。
  • @AayushKumarSingha - 添加了一些 cmets。这有帮助吗?
  • @vzwick 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多