这是我的HTML ...
$("table tr button").on('click', function(e) {
var i = $(this).closest('td').parent()[0].sectionRowIndex;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">
<tr>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
</tr>
<tr>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
<td><button class="button"></button></td>
</tr>
</table>
我试图获取被点击按钮的索引。
但是我遇到了错误...请有人告诉我该怎么做。
答案 0 :(得分:3)
由于您使用的是jQuery,因此可以简单地使用 index()
方法,例如:
行/表中按钮的索引:
$("table tr button").on('click', function(e) {
var index_in_row = $(this).closest('td').index();
var index_in_table = $('button').index($(this));
console.log('Index of the button in row: ' + index_in_row);
console.log('Index of the button in table: ' + index_in_table );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">
<tr>
<td><button class="button">button 0</button></td>
<td><button class="button">button 1</button></td>
<td><button class="button">button 2</button></td>
<td><button class="button">button 3</button></td>
</tr>
<tr>
<td><button class="button">button 4</button></td>
<td><button class="button">button 5</button></td>
<td><button class="button">button 6</button></td>
<td><button class="button">button 7</button></td>
</tr>
</table>
答案 1 :(得分:3)
您可以在jquery中使用closest
和index
。每行的索引将从0开始,并且按钮的索引相对于该行。
您还可以通过将table
替换为$("table tr button").on('click', function(e) {
来将事件从$("table").on('click', 'button', function(e) {
委派给按钮
$("table").on('click', 'button', function(e) {
var rowIndex = $(this).closest('tr').index();
var colIndex = $(this).closest('td').index()
console.log(`Row index is ${rowIndex} & button index is ${colIndex}`)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">
<tr>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
</tr>
<tr>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
<td><button class="button">Click</button></td>
</tr>
</table>
答案 2 :(得分:0)
您可以将索引值存储在如下所示的自定义数据属性中
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">
<tr>
<td><button data-index='0' class="button">Click</button></td>
<td><button data-index='1' class="button">Click</button></td>
<td><button data-index='2' class="button">Click</button></td>
<td><button data-index='3' class="button">Click</button></td>
<td><button data-index='4' class="button">Click</button></td>
<td><button data-index='5' class="button">Click</button></td>
</tr>
<tr>
<td><button data-index='6' class="button">Click</button></td>
<td><button data-index='7' class="button">Click</button></td>
<td><button data-index='8' class="button">Click</button></td>
<td><button data-index='9' class="button">Click</button></td>
<td><button data-index='10' class="button">Click</button></td>
<td><button data-index='11' class="button">Click</button></td>
</tr>
</table>
然后单击按钮后,您可以通过jquery这样获得索引值
$('.button').click(function(){
let index = $(this).data('index');
console.log(index);
});