2015-07-12 25 views
1

我有一個表格,我想每次在行中觸發一個複選框時刪除一個表格。我有一行工作(只有當我特別輸入該行的ID)。有什麼方法可以爲javascript函數提供動態輸入嗎?我該怎麼做呢?.change函數的多個輸入

HTML(嵌入式紅寶石)

<tr class="showContainer <%= "hideTableRow#{comment.id}" %>"> 
    <th><%= check_box_tag "approved#{comment.id}", comment.id, comment.approved, data: { toggle: "hideTableRow#{comment.id}" }, class: 'comment-approve' %></th> 
    <th><%= comment.username %></th> 
    <th><%= comment.email %></th> 
    <th><%= comment.body %></th> 
</tr> 

這轉化爲(2項)。

<tr class="showContainer hideTableRow1"> 
    <th><input type="checkbox" name="approved1" id="approved1" value="1" data-toggle="hideTableRow1"></th> 
    <th>foobar username1</th> 
    <th>foobar email1</th> 
    <th>foobar body1</th> 
</tr> 

<tr class="showContainer hideTableRow3"> 
    <th><input type="checkbox" name="approved3" id="approved3" value="3" data-toggle="hideTableRow3"></th> 
    <th>foobar username3</th> 
    <th>foobar email3</th> 
    <th>foobar body3</th> 
</tr> 

我的CSS:

.showContainer { 
    display:block; 
} 

我的JS:

$("#approved1").change(function() { 
    $('.' + $(this).data('toggle')).toggle(); 
}); 

顯然,我的JS現在只適用於第一tablerow的。我該如何做到這一點,以便它們都能單獨影響?

對不起,如果這是一個總noob問題。學習JS是下一個,哈哈。

謝謝你的幫助!

+0

可以使用'$( 「輸入」)'或'$( 「輸入[類型=複選框]」)'代替'$( 「#approved1」)'。這解決了你的問題? – lmgonzalves

回答

1

你想選擇包含的行,是否正確?試試這個:

$('.approved-checkbox').change(function(e){ 
    $(e.target).closest('.showContainer').toggle(); 
}); 

然後給複選框的'approved-checkbox'或任何你想使用的名稱。

.closest() documentation

相關問題