如何根据另一个输入的值隐藏或显示表单输入?

时间:2015-08-31 20:11:44

标签: ruby-on-rails ruby forms

如何动态隐藏使用Rails表单助手构建的输入字段?例如,请考虑以下代码:

    <%= f.select :notification_type, [['Specific','Specific'],['Mass','Mass'],['All','All']] %>

    <%= f.label 'Enter the specific customer imei whom you want to send' %>
    <%= f.text_area  :imei %>

如何根据imei的值隐藏或显示notification_type文字区域?

1 个答案:

答案 0 :(得分:0)

如果您希望在不刷新页面的情况下进行此操作,则需要使用JS / jQuery。首先设置你的表单如下:

<%= f.select :notification_type, [['Specific','Specific'],['Mass','Mass'],['All','All']], class: "notification-type" %>
<%= f.label 'Enter the specific customer imei whom you want to send' %>
<%= f.text_area  :imei, class: "imei", display: none %>

然后在您的assets / javascript或views / action_name.coffee.erb中添加一些jQuery(本例中为CoffeeScript),如下所示:

$->
  $(".notification-type").on "select", ->
    if $(this).val() == "Mass" # replace the condition with the thing that should show the text area
      $(".imei").show()
    else
      $(".imei").hide()

这样的事情应该有用......虽然你可能需要玩它,因为我还没有测试过这段代码。