![](/img/trans.png)
[英]How to get Dropdownlist value on HTML Table Cell in a Row using javascript
[英]Html Table get row id by cell value
我想通过搜索单元格值从表中获取行 ID 或索引,但我不知道如何。 请帮助我开始::)
<button id="button" class="btn btn-secondary">Get index of row 2</button>
<table id="table">
<tbody>
<tr data-index="0">
<td>cell 1</td>
<td>cell 2</td>
<td>cell 3</td>
</tr>
<tr data-index="1">
<td>cell 4</td>
<td>cell 5</td>
<td>cell 6</td>
</tr>
<tr data-index="2">
<td>cell 7</td>
<td>cell 8</td>
<td>cell 9</td>
</tr>
</tbody>
</table>
var $table = $('#table')
var $button = $('#button')
var $SearchFor ='cell 5'
$(function() {
$button.click(function () {
alert('row id of '+$SearchFor+' is')
})
})
您可以使用以下内容:
$table.find("td:contains('"+$SearchFor+"')").parent().index();
或者
$table.find("td:contains('"+$SearchFor+"')").parent().data("index");
演示
var $table = $('#table') var $button = $('#button') var $SearchFor = 'cell 5' $(function() { $button.click(function() { var i = $table.find("td:contains('"+$SearchFor+"')").parent().index(); console.log($SearchFor + " is found in row with data-index " +i) }) })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="button" class="btn btn-secondary">Get index of row 2</button> <table id="table"> <tbody> <tr data-index="0"> <td>cell 1</td> <td>cell 2</td> <td>cell 3</td> </tr> <tr data-index="1"> <td>cell 4</td> <td>cell 5</td> <td>cell 6</td> </tr> <tr data-index="2"> <td>cell 7</td> <td>cell 8</td> <td>cell 9</td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.