亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 JS 變量中存儲表行數

在 JS 變量中存儲表行數

C#
POPMUISE 2023-07-09 16:22:17
我有一些舊代碼正在嘗試瀏覽。該表是通過返回模型的控制器操作加載的,基于單擊頁面中其他位置的按鈕。如何在 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>這是我在 JS 中嘗試做的事情:var rowId = "#" + $(this).data("rowindex");    var row = $(rowId);    console.log(rowId);    console.log(row);console.log 的結果似乎不準確。任何事情都有幫助。謝謝
查看完整描述

2 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

也許您想要表格的行數。


// 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() );

});


查看完整回答
反對 回復 2023-07-09
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

你也可以像這樣使用 jquery 來做到這一點


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

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



查看完整回答
反對 回復 2023-07-09
  • 2 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號