【发布时间】:2022-06-01 22:43:31
【问题描述】:
我在我的 ASP.NET 网页中定义了以下引导表:
<table class="display table table-bordered" data-click-to-select="true"
data-pagination="true" data-sortable="true" data-show-refresh="true" data-single-select="true" data-maintain-selected="true"
data-show-toggle="true" data-id-field="customer_id" id="customers" name="customers">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="Customer_ID" data-sortable="true">Acct. #</th>
<th data-field="Company_Name" data-sortable="true">Company</th>
<th data-field="Federal_EIN" data-sortable="true">EIN</th>
<th data-field="City" data-sortable="true">City</th>
<th data-field="State" data-sortable="true">State</th>
<th data-field="Creation_Date" data-sortable="true">Added</th>
</tr>
</thead>
</table>
而且,按照 SO 上的示例代码,我正在尝试使用以下 jQuery/JavaScript 代码从所选行中获取和显示信息:
$('#customers').on('check.bs.table', function (e, row) {
checkedRows.push({ id: row.id, name: row.name, forks: row.forks });
console.log(checkedRows);
$.each(checkedRows, function (index, value) {
$(console.log(value.id + " | " + value.name + " | " + value.forks));
});
});
$('#customers').on('uncheck.bs.table', function (e, row) {
$.each(checkedRows, function (index, value) {
if (value.id === row.id) {
checkedRows.splice(index, 1);
}
});
console.log(checkedRows);
});
问题是,控制台将选中行的值显示为undefined。我究竟做错了什么?
【问题讨论】:
-
checkedRows是什么?这在任何地方都没有定义。我认为我们需要查看更多您的代码。此代码是否在元素存在之前触发? -
checkRows 在前面的代码中定义为
var checkedRows = [];。除此之外,javascript 中唯一的其他代码是用于加载表格的搜索功能。 -
你应该看看how to create a minimal reproducible example。我们确实需要更多信息,因为我们不知道行数据是什么。我们获得的信息越多,我们就越有可能为您提供帮助。
标签: javascript jquery bootstrap-table