Javascript 在JS变量中存储表行数

Javascript 在JS变量中存储表行数,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,我有一些旧的代码,我正试图刺穿。此表通过返回模型的控制器操作加载,该操作基于页面中其他位置的按钮单击 如何在JS变量中找到表中的行数?我对JS很糟糕。我尝试了一些事情,但没有任何效果。下面是我的代码,也是我尝试存储行数的方法 表: <hr /> <div class="row" id="ReceiptsMainDiv"> <div class="col-md-12" style="overflow-y:scroll"> <tabl

我有一些旧的代码,我正试图刺穿。此表通过返回模型的控制器操作加载,该操作基于页面中其他位置的按钮单击

如何在JS变量中找到表中的行数?我对JS很糟糕。我尝试了一些事情,但没有任何效果。下面是我的代码,也是我尝试存储行数的方法

表:

<hr />
<div class="row" id="ReceiptsMainDiv">
    <div class="col-md-12" style="overflow-y:scroll">
        <table class="table table-striped table-hover table-bordered" id="terminalReceipts">
            <thead>
                <tr>
                    <th>Terminal ID</th>
                    <th>Local Transaction Time</th>
                    <th>Amount</th>
                    <th>Receipt</th>
                    <td class="hidden"></td>
                </tr>
            </thead>
            <tbody>

                @foreach (var item in Model.TransactionsTests)
                {
                    <tr id="@String.Concat("rowIndex", Model.TransactionsTests.IndexOf(item))">
                        <td>@item.TerminalID</td>
                        <td>@item.TransactionTime</td>
                        <td>@item.Amount</td>
                        @*<td>@Html.ActionLink("View Receipt", "ViewReceipt", new { id = item.Id }, new { @class = "btn btn-primary btn-sm" }) <br /></td>*@
                        <td class="transactionID hidden">@item.Id</td>
                        <td>
                            @if (item.ReceiptData == null)
                            {
                                <button class="btn btn-sm btn-primary viewReceipt" disabled>View Receipt</button>
                            }
                            else
                            {
                                <button class="btn btn-sm btn-primary viewReceipt" data-rowindex="@String.Concat("rowIndex", Model.TransactionsTests.IndexOf(item))">View Receipt</button>
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>

console.log的结果似乎不准确。什么都行。谢谢

您可能需要表格的行数

// javascript
var rowsInTable = document.getElementById("terminalReceipts").getElementsByTagName("tr").length;

//jquery
var rowsInTable2 = $("#customers").children('tbody').children('tr').length;

//if you need to do something with the rows:

var rows = $("#customers").children('tbody').children('tr');

rows.each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

您也可以像这样使用jquery来实现这一点

var totalRowCount = $("#terminalReceipts tr").length;            //this will give +1 row
var rowCount = $("#terminalReceipts td").closest("tr").length;   //this will give actual row

我已经很长时间没有使用Razor了,但是有没有办法访问
Model.transactionTests.Count
并将其存储在变量中?或者将计数添加到一个隐藏元素中,这样您就不必遍历整个表。我不确定这是否有效,但请尝试Model.transactiontests.count谢谢。工作完美。
var totalRowCount = $("#terminalReceipts tr").length;            //this will give +1 row
var rowCount = $("#terminalReceipts td").closest("tr").length;   //this will give actual row