2009-08-19 141 views
0

的前一个实例我已经表和按钮遍历DOM找到

---------------------- 
table 1 .inventory class 
---------------------- 
[button 1 .add-row] 

---------------------- 
table 2 .inventory class 
---------------------- 
[button 2 .add-row] 

---------------------- 
table 3 .inventory class 
---------------------- 
[button 3 .add-row] 
现在

以下重复模式当我点击按钮1,2,3我想遍历表1,2,3 (前面的表格),并在表格中显示一个额外的行(已经创建,只是隐藏),我试过但所有的按钮只影响第一个表格,一旦显示所有这些行,然后开始显示在下一个表格上等我知道答案可能很明显..继承人我的代码:

$(document).ready(function(){ 
    $('.additional-item-row').hide(); 
    $('.add-item').click(function(){ 
     $(this).prevAll(".inventory .additional-item-row:hidden:first").show(); 
    }); 
}); 

回答

1

试试这个:

$(document).ready(function(){ 
    $('.additional-item-row').hide(); 
    $('.add-item').click(function(){ 
     $(this).prev("table").children(".inventory .additional-item-row:hidden:first").show(); 
    }); 
}); 
1

好,我设法解决它:

$(this).prevAll(".inventory:first").find(".additional-item-row:hidden:first").show(); 
1

假设:在组按钮的按钮的索引表行的索引匹配的一组表中的行。

$(function() { 
    $("button.add-item").click(function() { 
     var index = $("button.add-item").index(this); 
     $("tr.additional-item-row:eq(" + index + ")").show(); 
    }); 
});