【问题标题】:rails observe_field using ActionView Helpersrails observe_field 使用 ActionView Helpers
【发布时间】:2009-09-16 12:08:00
【问题描述】:

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

  <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?客户端唯一等效的是什么?
  • true }, { :onchange => "$('otherform').show();" } %> 适用于整个表单

标签: javascript ruby-on-rails ruby forms select


【解决方案1】:

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

例如

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

【讨论】:

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

    知道了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 2011-01-29
      • 2015-12-29
      • 2020-08-14
      • 1970-01-01
      相关资源
      最近更新 更多