jQuery比较两个表中的行 [英] Jquery to compare rows in two tables
本文介绍了jQuery比较两个表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用jquery比较两个表的单元格中的值. 在表一的值中,我应该将名字,中间名和姓氏与表2进行比较.如果这三个值与表2的行匹配,那么我应该为第二个表中的行着色.
I have to compare values in the cells of two tables using jquery. Usin the values of the table one i should compare firstname, middlename and last name with table 2. If these 3 values match with table 2 row then i should color the row in second table.
Fiddle
<table id="T1" border='1'>
<thead>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Suffix</th>
</tr>
</thead>
<tbody>
<tr>
<td>12</td>
<td>34</td>
<td>56</td>
<td>78</td>
</tr>
</tbody>
</table>
<table id="T2" border='1'>
<thead>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Suffix</th>
</tr>
</thead>
<tbody>
<tr>
<td>12</td>
<td>34</td>
<td>56</td>
<td>23</td>
</tr>
<tr>
<td>bat</td>
<td>man</td>
<td>11</td>
<td>21212</td>
</tr>
<tr>
<td>james</td>
<td>bond</td>
<td>007</td>
<td>dadadada</td>
</tr>
<tr>
<td>12</td>
<td>34</td>
<td>56</td>
<td>78</td>
</tr>
</tbody>
</table>
<br /><br /><br /><br />
<button id="btn">color if matches</button>
推荐答案
尝试一下:
$(function(){
$('#btn').on('click', function(e){
$('#T1 tbody tr').each(function(){
var row = $(this);
var left_cols = $(this).find("td:lt(3)");
$('#T2 tbody tr').each(function(){
var right_cols = $(this).find("td:lt(3)");
if(left_cols.html() == right_cols.html()) {
left_cols.css('background-color', 'green').css('color', 'red');
right_cols.css('background-color', 'green').css('color', 'red');
}
});
});
});
});
希望这会有所帮助.
这篇关于jQuery比较两个表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文