jQuery:从表中选择相同的数字 [英] jQuery: Select identical numbers from a table
问题描述
我从MYSQL加载了这个HTML表,每一行都是这样组成的:
I have this HTML table loaded from MYSQL, every row is composed this way:
<tr><td>02/05/2015</td><td class="bari1">89.30.86.72.65</td></tr>
<tr><td>30/04/2015</td><td class="bari2">56.11.73.36.13</td></tr>
每行都有一个"bari"类,后跟一个ID(1到15).
Each row has a class 'bari' followed by an id (1 thru 15).
这是结果:
通过jQuery,我从最后两行创建了一个数字数组,因此该数组现在由以下组成:["9", "1", "43", "79", "28", "40", "16", "6"]
(删除了重复号码).
Thru jQuery I created an array of numbers from the last 2 lines, so the array is now composed of: ["9", "1", "43", "79", "28", "40", "16", "6"]
(removed the repeating number).
我必须找到并突出显示其他列中存在的重复数字.
I have to find and highlight the repeating numbers are present in the other columns.
所以我做到了:
function highlightNumbers(){
var due_serie = $('.bari15').html().split('.').concat($('.bari14').html().split('.'));
//Rimuovo i doppioni
var serieCompleta = [];
$.each(due_serie, function(i, el){
if($.inArray(el, serieCompleta) === -1) serieCompleta.push(el);
});
//Ottengo dati
var ba13 = $('.bari13').html().split('.');
$('.bari13').html('');
var found = 0;
for(i = 0; i<= ba13.length-1; i++){
for(n = 0; n<= serieCompleta.length-1; n++){
if(ba13[i] == serieCompleta[n]){
$('.bari13').append('<span style="color:red">'+ba13[i]+'</span>.');
found = ba13[i];
}
}
if(ba13[i] != found){
$('.bari13').append(ba13[i]+'.');
}
}
}
使用此代码,我突出显示了第13行中的数字,但我想不出一个for循环来完成整个表(15行)的操作.
With this code I highlighted the numbers in row #13, but I cannot think of a for loop to complete this for the whole table (15 rows).
推荐答案
尝试一下,
我已经更新了您自己的代码,以便您可以轻松理解.
I have updated your own code, so that you can understand it easily.
function highlightNumbers(){
var all= $("td[class*=bari]");
var index = all.length-1;
var due_serie = $(all[index]).html().split('.').concat($(all[index-1]).html().split('.'));
//Rimuovo i doppioni
var serieCompleta = [];
$.each(due_serie, function(i, el){
if($.inArray(el, serieCompleta) === -1) serieCompleta.push(el);
});
//Ottengo dati
for(var s=0;s<index-1;s++)
{
var bar = $(all[s]);
var barnum = bar.html().split('.');
bar.html('');
var found = 0;
for(i = 0; i<= barnum.length-1; i++)
{
for(n = 0; n<= serieCompleta.length-1; n++)
{
if(barnum[i] == serieCompleta[n])
{
bar.append('<span style="color:red">'+barnum[i]+'</span>.');
found = barnum[i];
}
}
if(barnum[i] != found)
{
bar.append(barnum[i]+'.');
}
}
}
}
highlightNumbers();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><td>02/05/2015</td><td class="bari1">89.10.86.30.65</td></tr>
<tr><td>30/04/2015</td><td class="bari2">96.11.73.36.13</td></tr>
<tr><td>02/05/2015</td><td class="bari3">78.34.50.72.11</td></tr>
<tr><td>30/04/2015</td><td class="bari4">34.78.69.60.22</td></tr>
<tr><td>02/05/2015</td><td class="bari5">12.29.30.69.33</td></tr>
<tr><td>30/04/2015</td><td class="bari6">59.10.20.96.44</td></tr>
</table>
这篇关于jQuery:从表中选择相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!