gpt4 book ai didi

javascript - rails observe_field 使用 ActionView Helpers

转载 作者:数据小太阳 更新时间:2023-10-29 07:31:20 25 4
gpt4 key购买 nike

我有一个带有几个选项的选择菜单

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

最后一个是 id "5"和 name = "Other"

当且仅当他们选择其他时,我希望显示一个带有 div id = "other"的隐藏 text_area 以允许用户键入...

我怎样才能简单地做到这一点?

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

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

最佳答案

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

例如

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

关于javascript - rails observe_field 使用 ActionView Helpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1432569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com