检查表行列中的值 [英] check value in table row column
问题描述
我有一个html表-TemplateData.在Tbody内部,每行"data-uid"上都有一列,该列可能不是唯一的.在对表格进行升序显示之前,我已经对其进行了排序.看起来像这样:
I have a html table - TemplateData. Inside the Tbody I have a column on each row 'data-uid', which may not be unique. I have sorted the table before it is displayed in ascending order. It looks like so:
<tr id="Tr2" data-uid="1" role="row" class="odd"/>
<tr id="Tr2" data-uid="2" role="row" class="even"/>
<tr id="Tr2" data-uid="2" role="row" class="odd"/>
<tr id="Tr2" data-uid="2" role="row" class="even"/>
<tr id="Tr2" data-uid="3" role="row" class="odd"/>
<tr id="Tr2" data-uid="4" role="row" class="odd"/>
注意2出现3次.每个值可能会出现多次
NOTE 2 appears three times. Each value may appear more than once
我可以使用以下方法获取表格:
I can get the table by using:
var table = $('#TemplateData').dataTable();
我的问题是:如果我在'val'中存储了一个值,如何检查该表以获取带有data-uid=val;
My Question being: if I have a value stored in 'val' how can I check the table to get the row with data-uid=val;
所以...类似于table.rows [val]
so...something like table.rows[val]
从此处检查下一行,以查看其data-uid与val是否不同.如果是这样,请传回该值.如果没有找到,请继续进行下一行直到找到其他值.
From here check the next row to see if its data-uid is different from val. If so pass back this value. If not continue onto the next row until you find a different value.
所以......
val = 2
get table.row[where val = data-uid]
check next row
if table.rows[data-uid = val] continue until it is not the same, when value is different pass back
var newVal = ?
....任何帮助将不胜感激
....any help would be appreciated thanks
推荐答案
这是纯JavaScript程序,希望对您有所帮助.
This is with pure JavaScript, hope it helps.
var elements = document.getElementsByTagName('tr');
var elementsLength = elements.length;
for (var i = 0; i < elements.length; i++) {
var currentElement = elements[i].getAttribute('data-uid');
for (var j = i + 1; j < elements.length; j++) {
var nextElement = elements[j].getAttribute('data-uid');
if(currentElement !== nextElement) {
return elements[j].getAttribute('data-uid');
}
}
}
上帝的运气
Zorken17
这篇关于检查表行列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!