【问题标题】:Add custom class to form fields将自定义类添加到表单字段
【发布时间】:2015-03-22 18:56:56
【问题描述】:

有这样的形式

<%= form_tag 'search', method: 'get', class: 'form-horizontal' do %>
     <%= label_tag  :search, 'Enter url here' %>
     <%= text_field_tag :search  %>
     <%= submit_tag 'Get it!' %>
    <% end %>

上面的代码为form标签添加了class='form-horizontal'

如何为每个输入字段添加class='form-control'

【问题讨论】:

    标签: ruby-on-rails ruby forms ruby-on-rails-4


    【解决方案1】:

    试试这个:

    text_field_tag :search, nil, class: 'form-control'
    

    供将来参考:

    text_field_tag(name, value = nil, options = {})
    
    text_field_tag :search
    # => <input id="search" name="search" type="text" />
    
    text_field_tag :search, 'Enter your search query here'
    # => <input id="search" name="search" type="text" value="Enter your search query here" />
    
    text_field_tag :search, nil, class: 'form-control'
    # => <input class="form-control" id="search" name="search" type="text" />
    

    【讨论】:

      【解决方案2】:
      <%= form_tag 'search', method: 'get', class: 'form-horizontal' do %>
         <%= label_tag  :search, 'Enter url here' %>
         <%= text_field_tag :search, {:class => 'form-control'}  %>
         <%= submit_tag 'Get it!' %>
      <% end %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-25
        • 2014-09-24
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 2014-06-21
        • 2016-06-15
        • 2012-10-02
        相关资源
        最近更新 更多