javascript - 获取html表格每一行输入字段的值
问题描述
我有一个 html 表。每行都有带有隐藏边框的输入字段。当我按下一个按钮时,如果可能的话,它应该给我每个表格行的输入字段的所有值,而不使用 id 和 class。
html:
<div class="row" style="z-index:0;">
<div class="col-md-12">
<table id="detailTable" class="table table-bordered table-sm">
<thead class="thead-light" style="text-align:center">
<tr>
<th style="width:130px;">Date</th>
<th style="width:300px;">Particulars</th>
<th style="width:10px;">C.B.F</th>
<th style="width:180px;">Dr. Amount</th>
<th style="width:180px;">Cr. Amount</th>
<th style="width:200px;">Balance</th>
<th style="width:10px;">Opt</th>
</tr>
</thead>
<tbody>
<tr>
<td> <input type='text' value='11-12-2018'/> </td>
<td> <input type='text' value='Cash'/> </td>
<td> <input type='text' value=''/> </td>
<td> <input type='text' value='20000'/> </td>
<td> <input type='text' value='15000'/> </td>
<td> <input type='text' value='-5000'/> </td>
<td> <input type='button' value='Delete'/> </td>
</tr>
</tbody>
</table>
</div>
<button class='btn btn-success'>Show</div>
</div>
我试过的 JavaScript
var rowsInTable = $("#detailTable tr").length;
rowsInTable--;
row.parentNode.removeChild(row);
for(i=1;i<=rowsInTable;i++)
{
console.log(document.getElementById("detailTable").rows[i].innerHTML);
}
解决方案
由于 JQuery 没有被标记,这就是我只在 Javascript 中这样做的方式:
var tableRows = document.getElementById("detailTable").rows
for (i = 1; i < tableRows.length; i++) {
var myrow = tableRows[i];
for (j = 0; j < myrow.cells.length; j++) {
var mytd = myrow.cells[j];
console.log(mytd.children[0].value);
}
}
您可以进一步微调它以获得格式化的输出。PS:检查控制台的输出。
推荐阅读
- linux - packet_mmap 异步数据包处理
- arrays - 在 CUDA 内核中访问 2D 数据
- python - Spark:处理多个时间戳值
- elasticsearch - Elasticsearch 搜索精确标签
- android - 为什么工具栏中的 onBackPressed() 没有反应?
- python - 如何删除在 sqlalchemy 中重复的行
- android - Spotify 设计指南:Spotify 随机播放和点播播放
- cryptography - 加密链上数据来自哪里?
- ios - 如何在 textField 中仅切换自定义键盘 - Xcode
- java - 将动态方法名称作为java中的参数传递给另一个方法