2009-09-16 27 views
4

我有几个选择轨使用的ActionView助手

<p> 
    <%= f.label :reason %><br /> 
    <%= f.select :reason, Confirmation.reasons.collect {|p| [ p[:name], p[:id] ] }, { :include_blank => true } %> 
    </p> 

最后被ID =“5”和名称=“其他”

当且仅当他们选择其他的我想有一个选择菜单的observe_field一个div id =“other”的隐藏text_area被显示为允许用户键入...

我该怎么做到这一点?

<%= link_to_function "Show me other" do |page| 
    page[:other].toggle 
    end %> 

我可以切换它,但我想显示它在选择框中的“其他”?这是一个为observe_field工作吗?

+0

*道德*含义?! ;-) – 2009-09-16 12:17:55

+0

但observe_field使用ajax?客户方只有等同的东西? – holden 2009-09-16 13:03:22

+0

<%= f.select:reason,Confirmation.reasons.collect {| p | [p [:name],p [:id]]},{:include_blank => true},{:onchange =>“$('otherform')。show();” }%>适用于整个表格 – holden 2009-09-16 13:30:30

回答

3

你自己的回答很好,但为了将来的参考,observe_field不必进行Ajax调用。您可以使用:function指定要在本地运行的JavaScript代码。

例如

<%=observe_field 'reason', :frequency => 1, 
    :function => "if ($('reason').value == '5') { $('otherform').show(); } else { $('otherform').hide(); }" %> 
+0

哦,我喜欢你的。谢谢! – holden 2009-09-16 19:19:48

2
<%= f.select :reason, Confirmation.reasons.collect {|p| [ p[:name], p[:id] ] }, { :include_blank => true }, { :onchange => "if (this.value == '5') { $('otherform').show(); }" } %> 

Got it!