簡體   English   中英

仍支持Rails Collection_Select onchange嗎?

[英]Is Rails Collection_Select onchange still supported?

我正在嘗試創建一個僅當用戶從下拉菜單中選擇“其他”時才可見的文本字段。

我之前已經使用一些jQuery和rails“ select”做到了這一點,但是我無法使其適用於“ collection_select”。

人們說onChange應該有用,但到目前為止我還沒有運氣。

集合選擇:

  <%= f.collection_select(:professor, Professor.order(:fullname), :id, :fullname,
     include_blank: false, :prompt => "Other", :onchange => "review_professor();")%>
     <%= f.text_field :professor, class: "class-reveal", style: "display: none;"%>

Javascript:

    review_professor = function() {
     if(this.value == "Other") {
       $('.class-reveal').show();
       $('.class-reveal').val('');
     }else{
       $('.class-reaveal').hide();
       $('.class-reveal').val(this.value);
     }
   });

關於為什么這不起作用的任何想法,或者我可以這樣做的其他方式? 仍然支持collection_select嗎?

它仍然適用於collection_select,只需要進入html_options哈希 ,就像這樣:

{include_blank: false, prompt: "Other"}, {onchange: "review_professor();"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM