2014-01-28 83 views
0

我有一個包含3個值的下拉QUESTIONS_TYPES = { '單', '多重',A文本}顯示和隱藏塊而在下拉改變[Rails4,jquery的]

<div class="field"> 
    <%= f.label :question_type %><br> 
    <%= f.select :question_type, Question::QUESTIONS_TYPES, prompt: 'Select a question type' %> 
    </div> 

使用JQuery形式,此下拉菜單顯示其他字段(在其類爲.answers_list的塊中),具體取決於選擇值。

$(document).ready(function(){ 
    $('#question_question_type').change(function(){ 
    var qst_type = $('#question_question_type option:selected').text() 
     if (qst_type == 'Multiple' || qst_type == 'Single'){ 
      $('.answers_list').show(); 
     } else{ 
      $('.answers_list').hide(); 
     } 
    }); 
}); 

的事情是,當我通過鏈接訪問形式:

<%= link_to 'New Question', new_question_path %> 

我的下拉沒有響應jQuery的行動,一旦我刷新它的頁面。任何建議?

謝謝!

+0

你試過沒有現成的事件加入這個活動?我的意思是嘗試這種方式 $('#question_question_type')。change(function(){ var qst_type = $('#question_question_type option:selected')。text() if(qst_type =='Multiple'|| $('。answers_list')。show(); } else { –

+0

這樣做在兩種情況下都不起作用。作爲一個jQuery我認爲有必要準備好在頁面中調用任何事件。 – Moh

回答

0

使用

$(".target").toggle(); 

看到 Click here

+0

使用切換我的區塊也會隱藏,同時選擇「多個」或「單個」。我的問題是事件沒有響應,直到我刷新頁面。 – Moh