2010-01-31 54 views
0

嘿,我有一個代碼段在這裏:單擊複選框時隱藏/顯示內容?

<div class="label"><%= f.label :course %> <span>(Does this relate to a specific course?)</span></div> 
    <%= check_box_tag(:no_course) %> 
     <%= label_tag(:no_course, "None") %><br /> 
    <%= f.collection_select(:course_id, @current_account.courses.find(:all, :order => "name"), :id, :name) %> 

我怎麼可能使它所以當

<%= check_box_tag(:no_course) %> 

選擇了

<%= f.collection_select(:course_id, @current_account.courses.find(:all, :order => "name"), :id, :name) %> 

只顯示使用JavaScript?

謝謝!

回答

1

如果您正在使用jQuery做,這樣做在你的document.ready:

if(!$('.no_course:checked').length) { 
    $('myElementSelectedByClass').hide(); 
} 

我假設這裏原本是顯示和你是隱藏在頁面加載時。基本前提是獲取您的複選框DOM元素(在這種情況下,我通過類名獲得)並查看是否設置了checked屬性。如果沒有,然後隱藏它。