【问题标题】:Ruby on Rails simple_form prepend $ before input with custom labelRuby on Rails simple_form 在输入前添加自定义标签
【发布时间】:2014-08-17 14:07:59
【问题描述】:

我正在尝试在我的一个字段前面添加$,并且我能够成功地这样做,但没有自定义标签。这是有效的代码:

= f.input :amount do
    $
    = f.input_field :amount, :label => false, :hint => false, :wrapper => false

这让我想到了两个令人困惑的点。 1) 为什么:label => false时还会出现标签? 2)如果我将其更改为为什么会失败:

= f.input :amount do
    $
    = f.input_field :amount, label: 'This is my label', :hint => false, :wrapper => false

当我将标签更改为自定义标签时,$ 会移动到标签上方而不是字段前面。感谢所有帮助,谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby haml simple-form


    【解决方案1】:

    当你在做的时候

    = f.input :amount do
      $
      = f.input_field :amount, label: 'This is my label', :hint => false, :wrapper => false
    

    这会生成 html

    $
    <label for="amount">This is my label</label>
    <input id="amount" name=" " type="text" />
    

    我不确定,但我认为 simple_form 正在您的标签上应用一个 css 的 display:block,因为 here 标签是内联元素

    我认为你所需要的只是一些 CSS 魔法来解决你的问题

    = f.input :amount do
      = f.input_field :amount, label: 'This is my label', :hint => false, :wrapper => false
      %span.dollar &#36; 
    

    然后在您的输入字段上使用这个 css:

    #input_id{
      //here your input_id would be something like resource_amount you can check it in developer console to be sure
      display:inline-block;
    }
    
    .dollar{
      float: left;
    }
    

    JSFiddle

    【讨论】:

    • 这不是简单地在标签前面显示$吗?这不是我想要做的,我希望标签出现在输入字段上方,但我希望 $ 直接出现在字段本身的前面。
    • @haye321 你想让 $ 出现在你的输入字段前面吗?
    • 是的,通过我问题中的第一个 sn-p,我实现了这一点。我的问题是为什么我不能同时包含自定义标签。看来我需要二选一了。
    • 我尝试向所有适用于wrapper_html: { class: 'dollar_field' } 的字段添加一个类,但它似乎不起作用。
    • wrapper_html 会将类应用于输入和标签的包装器,它不会将该类应用于输入字段
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2014-10-14
    • 2013-01-07
    • 1970-01-01
    相关资源
    最近更新 更多