admin管理员组文章数量:1287931
i'm trying to loop through all the cells in a table and do a parison on the value.
var table = document.getElementById("assignedvlans");
alert(table);
alert($('#assignedvlans tbody tr').length);
for (var i = 0, cell; cell = table.cells[i]; i++) {
//iterate through all cells in table.
alert('in the loop');
alert(cell.val());
if (cell.val == IdforVlanToAdd)
{
alert('This vlan is already associated with the port.');
$bexit = true;
break;
}
}
When i test this code, the alert(table) code is working - it returns "object HTMLTableElement" and the alert for the table lengths returns 4, which is also correct. But the alert statements inside the loop never happen. Can you tell me where i'm going wrong with the loop control? Thanks.
i'm trying to loop through all the cells in a table and do a parison on the value.
var table = document.getElementById("assignedvlans");
alert(table);
alert($('#assignedvlans tbody tr').length);
for (var i = 0, cell; cell = table.cells[i]; i++) {
//iterate through all cells in table.
alert('in the loop');
alert(cell.val());
if (cell.val == IdforVlanToAdd)
{
alert('This vlan is already associated with the port.');
$bexit = true;
break;
}
}
When i test this code, the alert(table) code is working - it returns "object HTMLTableElement" and the alert for the table lengths returns 4, which is also correct. But the alert statements inside the loop never happen. Can you tell me where i'm going wrong with the loop control? Thanks.
Share Improve this question edited Jan 29, 2013 at 20:24 Jonathan M 17.5k9 gold badges60 silver badges94 bronze badges asked Jan 29, 2013 at 20:22 dotdot 15.7k43 gold badges128 silver badges260 bronze badges 3- Do you get any errors in the console? – War10ck Commented Jan 29, 2013 at 20:24
- table.cells won't work because the .cells are part of .rows not table directly. – Louis Ricci Commented Jan 29, 2013 at 20:25
-
2
Also
cell
is not a jQuery object, so it won't have aval()
method (it's also not aninput
of any kind, so you probably wanttext()
after making it a jQuery object), and I'm assuming thatcell.val
is a typo..? And why are you mixing jQuery and plain-JavaScript approaches? (It works, but it can make things more plicated.) – David Thomas Commented Jan 29, 2013 at 20:25
4 Answers
Reset to default 7table
contains rows[]
, which themselves contain cells[]
. You can't get the cells[]
directly from the table
.
You could use table.getElementsByTagName('td')
as a shortcut, provided there are no nested tables.
Otherwise, you should loop through each of the rows[]
and in that loop you can loop through the cells[]
.
var table = document.getElementById('assignedvlans'),
rows = table.rows, rowcount = rows.length, r,
cells, cellcount, c, cell;
for( r=0; r<rowcount; r++) {
cells = rows[r].cells;
cellcount = cells.length;
for( c=0; c<cellcount; c++) {
cell = cells[c];
// now do something.
}
}
Try like this :
var $table = $('#assignedvlans tbody td');
$.map($table,function(){
if ($(this).text() == IdforVlanToAdd)
{
alert('This vlan is already associated with the port.');
return;
}
});
The reason you're not getting any alerts inside the loop is your for
loop is not structured correctly. It quits immediately because the assignment cell = table.cells[i]
returns false.
var myDataArr = [];
$('#dynamic_cards tr').each(function(){
$(this).find('td').each(function(){
myDataArr.push($(this).text());
});
});
console.log(myDataArr);
本文标签: jqueryjavascript looping through all cells in tableStack Overflow
版权声明:本文标题:jquery - javascript: looping through all cells in table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741297868a2370921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论