如何检查数据表是否有任何包含字符串的行并获取它的行索引

How to check if datatable has any row containing a string and get row index of it

本文关键字:字符串 索引 获取 任何包 何检查 检查 是否 数据表      更新时间:2023-09-26

我的html中有一个表格,其中包含用户名和密码列。 如何在 JavaScript 中使用 jquery 获取用户名="abc" 的行的索引?

var myTable = document.getElementById('usersTable');

使用filter()函数。如果您正在寻找完全匹配,请不要使用 :contains

$allRows = $('#usersTable tr');
row = $allRows.filter(function(){
    return $.trim($(this).find('td').eq(INDEX_OF_USERNAME_COLUMN).text()) == "abc";
});
index = $allRows.index(row);