传递表ID时通过循环

我有四个HTML表,必须比较一个表中的数据和用户选择的表。 我将用户选择的表ID传递给此函数,但我不知道如何遍历此表的行:

function callme(code) { var tableName = 'table'+code; //alert(tableName); //How to do loop for this table?? HELP!! $('#tableName tr').each(function() { //how to do if (!this.rowIndex) return; // skip first row var customerId = $(this).find("td").eq(0).html(); alert(customerId); // call compare function here. }); } 

对于经验丰富的jQuery程序员来说,它应该是非常简单的。 这是我的jsfiddle: http : //jsfiddle.net/w7akB/66/

您正在使用错误的选择器,这:

 $('#tableName tr') 

表示从具有id tableName表中获取所有tr 。 这是你想要做的:

 $('#' + tableName +' tr') 

所以你将选择id存储在tableName变量中的表。

选择器只是一个字符串。 您可以将这些部分与变量连接在一起:

 $('#' + tableName + ' tr').each(function() { // ... }); 

我用这个改变更新了你的jsfiddle 。