【发布时间】:2019-09-08 09:35:56
【问题描述】:
实际上,我正在使用一个函数,我单击表格中的任何单元格都会给出其行索引,但我不想要它。我想如果我点击最后一列的单元格,它会返回该单元格的行索引,但只有最后一列的单元格应该有这个功能,而不是其他的
$('#tbl tbody').on( 'click', 'td', function () {
var row = $(this).parent().parent().children().index($(this).parent());
alert(row);
});
通过使用此功能,我单击任何单元格,它会返回行索引 这是我的表的代码
<table id="tbl" class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Auth Provider</th>
<th scope="col">Auth Unique Id</th>
<th scope="col">Match Id</th>
<th scope="col">Game Mode</th>
<th scope="col">Character 1</th>
<th scope="col">Character 2</th>
<th scope="col">Character 3</th>
<th scope="col">Character 4</th>
<th scope="col">Character 5</th>
<th scope="col">Character 6</th>
<th scope="col">Match Time</th>
<th scope="col">Action</th>
<th scope="col">Schedule Match</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
$(document).ready(function() {
var trHTML = "";
$.ajax({
url: '../php/admin/schedule_match.php',
type: 'post',
data: {},
success: function(response) {
var data = $.parseJSON(response);
var table;
//table.clear();
table = $('#tbl').DataTable();
table.clear();
if(data!='') {
$.each(data, function(i, item) {
table.row.add([ data[i].name, data[i].provider,data[i].auth,data[i].mid,data[i].mode,'<img width="100px" height= "70px" src=' + data[i].p1 + '>','<img width="100px" height= "70px" src=' + data[i].p2 + '>','<img width="100px" height= "70px" src=' + data[i].p3 + '>','<img width="100px" height= "70px" src=' + data[i].p4 + '>','<img width="100px" height= "70px" src=' + data[i].p5 + '>','<img width="100px" height= "70px" src=' + data[i].p6 + '>',data[i].time,'<button class="btn btn-primary delete" data-id-id=' + data[i].id + '>Delete</button>','<button class="btn btn-primary schedule" data-id-id=' + data[i].id + '>Schedule</button>']);
});
table.draw();
}
}
})
});
现在这是代码,每当我单击一行时,它都会返回该行的全部数据
var table = document.getElementsByTagName("table")[0];
var tbody = table.getElementsByTagName("tbody")[0];
tbody.onclick = function (e) {
e = e || window.event;
var data = [];
var target = e.srcElement || e.target;
while (target && target.nodeName !== "TR") {
target = target.parentNode;
}
if (target) {
var cells = target.getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
data.push(cells[i].innerHTML);
}
}
alert(data);
};
我只想在行中最后一个可点击的单元格,当我最后点击时,我返回该行的数据,该行中的最后两个单元格除外
【问题讨论】:
-
你能分享你的
<table>的代码吗? -
@VermaJr。我有共享代码
-
检查这是否对您有帮助 - stackoverflow.com/questions/40298501/…
-
你正在将原生 JavaScript API 与 jQuery 混合,不要使用 jQuery。
-
查看代码的最后一部分并阅读描述...你能帮我吗?
标签: javascript jquery html