簡體   English   中英

如何在 Formtastic 中隱藏條件輸入?

[英]How to hide input on conditions in Formtastic?

我繼承了一個在 Rails 中使用 ActiveAdmin 和 Formtastic 的項目。 我對其中任何一個的經驗都很少,但它似乎很簡單,可以完成簡單的任務。

所以,我的任務是添加一個收音機輸入,但如果收音機與某個值不匹配,則隱藏另一個輸入。 我到處搜索,似乎找不到任何關於此的內容。

這是我的表格:

form do |f|
  f.inputs 'Book Details' do
    f.input :name, required: true
    f.input :has_subtitle, as: radio, required: true
    f.input :subtitle
  end
end

在這種形式中,我只需要顯示 :subtitle,如果 :has_subtitle 設置為 true/yes,否則他們不應該看到輸入(如果它是可見的,它也應該是必需的)。

這可以在 Formtastic 中完成嗎?

謝謝!

f.input :has_subtitle, as: radio, required: true, { data: { toggle: 'subtitle_input' }}

然后將javascript(這是coffeescript)添加到一個單獨的文件(例如form.js.coffee)

$(document).on 'ready page:load', ->
  $('[data-toggle]').on "change", ->
    $($(this).data('toggle')).toggle($(this).val() == 'show')

您可能需要對此進行一些配置,具體取決於單選按鈕的值

然后將其包含在 application.js 中

# application.js
//= require form

暫無
暫無

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

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