[英]How to get the count of rows from a dynamic html table?
每当用户完成表单时,我都会添加一个空的tbody。 最终这个表数据将存储在一个数据库中,但现在却不是。 这是我的表设置:
<div class="formContainer">
<form id="form1">
<b>Step Number: </b><input type="number" id = "theStep" name="step" required></br>
<b>First Name: </b> <input type="text" name="firstName" required></br>
<b>Last Name: </b><input type="text" name="lastName" required></br>
<b>Alias: </b><input type="text" name="alias" required></br>
<b>Today's Date: </b><input type="date" id="theDate" name="submitDate" required></br>
<input type="submit" name="sub" value="submit">
</form>
</div>
<h2>Step Approvals</h2>
<div class="table-responsive">
<table id="stepTable" class="table table-striped table-sm">
<thead>
<tr>
<td>Step</td>
<td>First Name</td>
<td>Last Name</td>
<td>Alias</td>
<td>Submission Date</td>
</tr>
</thead>
<tbody id="displayArea"></tbody>
</table>
</div>
我想用条目号自动填写表单,所以我试图通过计算表中的行数来跟踪有多少条目。 这是我尝试过的,但即使我向表中添加条目/行,它也只给了我一个'null'值:
var setStep = $('#stepTable > tbody >tr').length;
if (setStep = 'null') {
$("#theStep").attr("value", 1)
} else {
$("#theStep").attr("value", setStep + 1)
};
每次切换表单时,都应使用表格+ 1中的行数更新“步骤”字段。
您可以使用document.getElementById("myTable").rows.length
获取表的行号document.getElementById("myTable").rows.length
var rowCount = document.getElementById("myTable").rows.length; // For rows var colCount = document.getElementById("myTable").rows[0].cells.length; // For columns document.getElementById("count").innerHTML = "ROW COUNT: " + rowCount; document.getElementById("count").innerHTML += "<br/>COL COUNT: " + colCount;
<table id="myTable" border="1"> <tr> <td> ROW 1 </td> </tr> <tr> <td> ROW 2 </td> </tr> </table> <div id="count"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.