我有下表:
<table>
<tr>
<th>Alarm</th>
</tr>
<tr>
<td>OK</td>
</tr>
<tr>
<td>Missing</td>
</tr>
<tr>
<td>OK</td>
</tr>
</table>
我想像这样计算这个表中的行数
OK = 2
缺少= 1
但是我不知道如何使用jQuery,应该只为警报列指定选择器,因为我有其他列包含ok和missing
注意:我想计算表中特定列的行,仅用于报警列 alaram列包含2个ok和1个缺失。
答案 0 :(得分:6)
假设Alarm
是第一列:
var okCount = $('td:contains("OK")').filter(function() {
return $(this).index() == 0;
}).length;
var missingCount = $('td:contains("Missing")').filter(function() {
return $(this).index() == 0;
}).length;
console.log('okCount: ' + okCount);
console.log('missingCount : ' + missingCount);
答案 1 :(得分:4)
看看这是否有效:http://jsfiddle.net/AG6kr/
var table = document.getElementById('yourTableID'),
column = 1,
tr = table.getElementsByTagName('tr'),
trLen = tr.length,
numMissing = 0,
numOK = 0;
while(trLen--) {
if(tr[trLen].children[column-1].innerHTML == 'OK') {
numOK++;
}
if(tr[trLen].children[column-1].innerHTML == 'Missing') {
numMissing++;
}
}
console.log(numOK);
console.log(numMissing);
如果有人想知道怎么做sans jQuery
答案 2 :(得分:1)
谁需要jQuery?
var table = document.getElementById('yourTableID'),
cells = table.getElementsByTagName('td'),
numCells = cells.length,
okCount = 0,
missingCount = 0;
while(numCells--) {
if (cells[numCells].innerHTML == 'OK') okCount++;
else if (cells[numCells].innerHTML == 'Missing') missingCount++;
}
console.log('OK: ' + okCount + ', Missing: ' + missingCount); // Should output "OK: 2, Missing: 1 into the console
答案 3 :(得分:1)
不需要为索引过滤,使用第n个子选择器!我的选择器也使用thead和tbody,它会在OP代码中使用的示例中失败。
<强> HTML 强>
<table>
<thead>
<tr>
<th>Alarm</th>
<th>Warning</th>
</tr>
</thead>
<tbody>
<tr>
<td>OK</td>
<td>OK</td>
</tr>
<tr>
<td>Missing</td>
<td>Missing</td>
</tr>
<tr>
<td>OK</td>
<td>OK</td>
</tr>
</thead>
</table>
<强>的JavaScript 强>
var index = $("table thead th:contains('Alarm')").index() + 1; //index is zero based, nth-child is 1 based.
var cells = $("table tbody td:nth-child(" + index + ")");
var okCount = cells.filter(":contains('OK')").length;
var missingCount = cells.filter(":contains('Missing')").length;
console.log(okCount, missingCount);
答案 4 :(得分:0)
为了矫枉过正,这是因为你有相同的表格结构,并希望在任何列中搜索任何数据集。
HTML:
<table>
<tr>
<th>Alarm</th>
<th>Fruit</th>
</tr><tr>
<td>OK</td>
<td>Apple</td>
</tr><tr>
<td>Missing</td>
<td>Orange</td>
</tr><tr>
<td>OK</td>
<td>Apple</td>
</tr>
</table>
JavaScript的:
$(document).ready(function() {
var results = tallyResults($('table'), 'Fruit', Array('Apple', 'Orange'));
/*
{
Apple: 2,
Orange: 1
}
*/
});
function tallyResults(table, col, vals) {
var valsCount = { }
var columnIndex = null;
//-- init value object using vals array
$(vals).each(function(i, v) {
valsCount[v] = 0;
});
//-- get index of the column we are currently searching
table.find('tr th').each(function(i, v) {
el = $(this);
if (el.text() == col) {
columnIndex = el.index();
}
});
//-- tally all instances of the values we are searching
//-- for in the proper column index
table.find('tr td').each(function(i, v) {
el = $(this);
if (el.index() == columnIndex) {
t = el.text();
if(valsCount[t] != undefined) {
valsCount[t]++;
}
}
});
return valsCount;
}
答案 5 :(得分:0)
$('td:contains("OK")').length
将返回包含&#34; OK&#34;
答案 6 :(得分:-1)
这是你要找的吗? http://jsfiddle.net/tQTDk/9/
var points = 0;
$("td").each(function() {
var tdVal = $(this);
if(tdVal.text().indexOf('OK') >= 0)
points += 2;
if(tdVal.text().indexOf('Missing') >= 0)
points += 1;
});
alert(points);