javascript-循环遍历表并检查值
背景信息 我有两个表,一个是可用用户的列表,称为“用户”,另一个称为“选定的用户”。单击用户表中的一行时,应用程序会从所选的用户表中添加/删除记录javascript-循环遍历表并检查值,javascript,jquery,html,Javascript,Jquery,Html,背景信息 我有两个表,一个是可用用户的列表,称为“用户”,另一个称为“选定的用户”。单击用户表中的一行时,应用程序会从所选的用户表中添加/删除记录 <table id=users> <tr class="odd" role="row" id="row_89"><td>89</td><td>John Doe</td><td>23819</td><td>mm2@yahoo.com</td
<table id=users>
<tr class="odd" role="row" id="row_89"><td>89</td><td>John Doe</td><td>23819</td><td>mm2@yahoo.com</td></tr>
<tr class="even" role="row" id="row_90"><td>90</td><td>John Doe</td><td>23819</td><td>36338</td></tr>
<tr class="odd" role="row" id="row_91"><td>91</td><td>Jane Doe</td><td>23820</td><td>jane@yahoo.com</td></tr>
<tr class="even" role="row" id="row_92"><td>92</td><td>Jane Doe</td><td>23820</td><td>28519</td></tr>
<tr class="odd" role="row" id="row_93"><td>93</td><td>Jim Bob</td><td>23801</td><td>jbob@yahoo.com</td></tr>
</table>
<table id=selected_users class="table table-condensed table-bordered" width="80%">
<tr class="info"><td colspan="4"><b>Selected Users:</b></td></tr>
</table>
我不知道如何查找第三列是否与tds.eq(2.text()中的内容匹配
如有任何建议,将不胜感激
编辑1
这就是我到目前为止所做的:
//2015.06.11 - find and add all other rows with matching p number
var thisTR = $(this);
console.log(thisTR);
//select all siblings with same value in third column
thisTR.siblings().filter(function() {
console.log($('td',this).eq(2).text() );
//console.log($('td', thisTR).eq(2).text());
if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) {
console.log("i found a match");
console.log("what is the row for: " + $('td',this).eq(2).text());
};
});
我只需要一种方法来识别找到匹配td的行,然后执行类似于我正在执行的操作,将行添加到选定的用户:
var-tr;
tr=”“;
... 找到行,然后。。。
tr.append(“+tds.eq(0.text()+”);
tr.append(“+tds.eq(1.text()+”);
tr.append(“+tds.eq(2.text()+”);
tr.append(“+tds.eq(3.text()+”);
tr.attr('id',id.substring(4));
$(“#所选#用户”)。追加(tr) 我的建议是使用
数据-
自定义属性将用户ID添加到元素中,以便您可以使用选择器轻松访问它们
例如,请注意下面的数据uid
属性:
<tr class="odd" role="row" id="row_89" data-uid="23819"><td>89</td><td>John Doe</td><td>23819</td><td>mm2@yahoo.com</td></tr>
以下是您可以使用的-
@PeterAK,请查看我的编辑1。幸运的是,过滤功能需要返回
true
或false
,因此警报没有起到任何作用:(而且,我还意识到我的代码中有一个输入错误,我很快就会解决……在return
语句中将=
更改为=
。纠正输入错误应该会清除错误。问题是,一旦找到匹配的单元格,我需要能够识别该单元格所属的行,我可以将数据从该行复制到另一行。)e另一个表。是否要复制整行?也许可以将匹配的行分配给一个变量,克隆它们并将它们附加到另一个表中。如果您查看我的代码,在新代码之前,您将看到一些逻辑,其中我创建了一个,并将一系列的附加到它,然后将其添加到所选的用户表中。我计划这样做方法与其他具有相同pid的行类似。我喜欢此解决方案。它非常干净。但问题是我使用的是jquery dataTables,我认为我无法指定自定义属性。@dot您应该可以这样做。请参阅
//2015.06.11 - find and add all other rows with matching p number
var thisTR = $(this);
console.log(thisTR);
//select all siblings with same value in third column
thisTR.siblings().filter(function() {
console.log($('td',this).eq(2).text() );
//console.log($('td', thisTR).eq(2).text());
if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) {
console.log("i found a match");
console.log("what is the row for: " + $('td',this).eq(2).text());
};
});
<tr class="odd" role="row" id="row_89" data-uid="23819"><td>89</td><td>John Doe</td><td>23819</td><td>mm2@yahoo.com</td></tr>
rows = $('#users tr[data-uid=' + uid + ']');
// ...
//save a reference of the current row
var thisTR = $(this);
//select all siblings with same value in third row
thisTR.siblings().filter(function() {
return $('td',this).eq(2).text() == $('td', thisTR).eq(2).text();
})
//Iterate through them and do what needs to be done.
.each(function() {
//do something
//here 'this' refers to tr (matched row)
//$('td', this) should give you all the tds in 'this' row
});