我可以从行中获取第一个和最后一个值,但无法获取行中的第二个和第三个值。谁能帮我。
这是我的代码
=> 网页
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
<td>four</td>
<td><button class="btnDelete">Delete</button></td>
</tr>
=> JavaScript
$(".btnDelete").click(function (evt) {
var cell=$(evt.target).closest("tr").children().first();
var cell2=$(evt.target).closest("tr").children().last();
var custID=cell.text();
var custID2=cell2.text();
alert(custID);
alert(custID2);
}
thanks .
我认为在没有 jQuery 的情况下获得这个值更容易。通过使用HTMLTableRowElement.cells https://developer.mozilla.org/en/docs/Web/API/HTMLTableRowElementDOM 属性。这几乎就像一个数组,但又不是一个数组。
$("#myTable").on('click','.btnDelete',function(){
// get the current row
var currentRow = $(this).closest("tr")[0];
var cells = currentRow.cells;
var firstCell = cells[0].textContent;
var secondCell = cells[1].textContent;
//...
//nthCell = cells[n-1].textContent;
console.log( firstCell );
console.log( secondCell );
});
如果你仍然想要 jQuery,那么而不是.first()
and .last()
方法,你可以使用.eq() https://api.jquery.com/eq/ method.
var rowCells = $(this).closest("tr").children();
var firstCell = rowCells.eq( 0 ).text();
var secondCell = rowCells.eq( 1 ).text();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)