我无法找到问题的正确答案。我在表中有行,每行包含一个按钮。如何找到我刚刚点击的按钮的行索引。
<tr>
<td>111</td>
<td>aaa</td>
<td><input type="submit" name="first" id="first" value="First" onclick="countRow(this.id)" /></td>
</tr>
<tr>
<td>222</td>
<td>bbb</td>
<td><input type="submit" name="sec" id="second" value="Second" onclick="countRow(this.id)" /></td>
</tr>
<tr>
<td>333</td>
<td>ccc</td>
<td><input type="submit" name="third" id="third" value="Third" onclick="countRow(this.id)" /></td>
</tr>
答案 0 :(得分:1)
使用tbody标签和此javascript
function countRow(id) {
var table = document.getElementById('dataTable')
var tbody = table.getElementsByTagName('tbody')[0]
var rows = tbody.getElementsByTagName('tr');
for (i = 0; i < rows.length; i++) {
rows[i].onclick = function() {
alert(this.rowIndex + 1);
}
}
}
&#13;
<table id="dataTable">
<tbody>
<tr>
<td>111</td>
<td>aaa</td>
<td>
<input type="submit" name="first" id="first" value="First" onclick="countRow(this.id)" />
</td>
</tr>
<tr>
<td>222</td>
<td>bbb</td>
<td>
<input type="submit" name="sec" id="second" value="Second" onclick="countRow(this.id)" />
</td>
</tr>
<tr>
<td>333</td>
<td>ccc</td>
<td>
<input type="submit" name="third" id="third" value="Third" onclick="countRow(this.id)" />
</td>
</tr>
</tbody>
</table>
&#13;
答案 1 :(得分:1)
如果我们将
indexOf
转换为数组,请使用nodeList
数组原型。
将参数传递为this
(当前元素)而不是this.id
试试这个:
function countRow(elem) {
var trElement = elem.parentElement.parentElement;
var tr = document.getElementsByTagName('tr');
tr = Array.prototype.slice.call(tr);
alert(tr.indexOf(trElement));
}
<table>
<tr>
<td>111</td>
<td>aaa</td>
<td>
<input type="submit" name="first" id="first" value="First" onclick="countRow(this)" />
</td>
</tr>
<tr>
<td>222</td>
<td>bbb</td>
<td>
<input type="submit" name="sec" id="second" value="Second" onclick="countRow(this)" />
</td>
</tr>
<tr>
<td>333</td>
<td>ccc</td>
<td>
<input type="submit" name="third" id="third" value="Third" onclick="countRow(this)" />
</td>
</tr>
</table>
答案 2 :(得分:1)
$('input[type=submit').click(function() {
var trid = $(this).closest('tr').attr('id'); // table row ID
$('div').text(trid);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr id="row1">
<td>111</td>
<td>aaa</td>
<td><input type="submit" name="first" id="first" value="First" /></td>
</tr>
<tr id="row2">
<td>222</td>
<td>bbb</td>
<td><input type="submit" name="sec" id="second" value="Second" /></td>
</tr>
<tr id="row3">
<td>333</td>
<td>ccc</td>
<td><input type="submit" name="third" id="third" value="Third" /></td>
</tr>
</table>
<div id="show" style="color: blue">Show the id of the current clicked row: </div>
答案 3 :(得分:0)
尝试将此用于表格单元格选择器和所选单元格的操作
http://www.redips.net/javascript/table-td-merge-split/
答案 4 :(得分:0)
我假设你想知道<input>
元素的id,
<td><input type="submit" name="first" id="first" value="First" onclick="countRow(this.id)" /></td>
所以Onclick你想得到第一个&#39;对于上述元素。
所以为此修改元素 -
<td><input type="submit" name="first" id="first" value="First" onclick="countRow()" /></td>
在JavaScript中:
function countRow(){
// This would give you the id
event.target.id
}