2014-08-27 33 views
0

嵌套領域這是對以下問題的後續問題,主要是因爲我嘗試使用一些就可以瞭解決方案,並不能認爲我的出路:顯示/隱藏在新增加的領域

Change only the image inside same div when option from dropdown is chosen

鑑於有許多問題一個形式,你可以選擇設置的問題是選擇題,並添加答案的話,我有下列問題視圖(field_answers方法只呈現部分):

.nested-fields 
    = f.input :question 
    = f.input :type 
    #answers{:style => "display:none;"} 
    = f.simple_fields_for :answers do |m| 
     = field_answers(m) 
    %div{:class => "#links"} 
     = link_to_add_association '+', f, :answers, :partial => 'layouts/form/answers', :class => 'link-add-field' 
    = link_to_remove_association "remove question", f 

以下爲answ ERS:

.fields.answers-nested-fields 
    #answer-field 
    = f.input :answer 
    = link_to_remove_association "-", f, { wrapper_class: 'answer-nested-fields', :class => 'link-add-field remove_nested_fields' } 

我questions.js.coffee具有以下回調:

$('#type').change -> 
    if $(this).val() is "text" 
    $("#answers").hide() 
    else 
    $("#answers").show() 

$(document).ready -> 
    $('#questions').on("cocoon:before-insert", (e, question_to_be_added) -> 
    question_to_be_added.fadeIn "slow" 
).on("cocoon:after-insert", (e, added_question) -> 
    added_question.css "background", "red" 
).on "cocoon:before-remove", (e, question) -> 
    # allow some time for the animation to complete 
    $(this).data "remove-timeout", 5000 
    question.fadeOut "slow" 

的第一位是什麼工程,使回答的第一個問題顯示或隱藏,顯然這是行不通的與新增加的問題,因爲我想我會需要之前/之後插入回調來創建一個事件,其中顯示/隱藏答案字段被解僱,當問題類型被改爲任何非「文本」,所以我如何觀察關於問題類型屬性的更改事件?

回答

0

想出了一個解決方案,它實際上是一個jQuery的問題,我不得不使用.find()

$(document).ready -> 
    $('#questions') 
).on("cocoon:after-insert", (e, added_question) -> 
    added_question.find('#type').change -> 
     if $(this).val() is "text" 
     added_question.find("#answers").hide() 
     else 
     added_pergunta.find("#answers").show() 
)