【问题标题】:How to control the size of the select box in Ruby on Rails 3?如何控制 Ruby on Rails 3 中选择框的大小?
【发布时间】:2011-05-24 11:46:14
【问题描述】:

我这样创建选择框:

f.select(:my_select_name, options_for_select(...))

如何控制选择框的大小(宽度)?

对于输入文本字段我这样做:

f.text_field(:field_name, :size => my_size)

但是对于“选择”它不起作用。

请指教。

【问题讨论】:

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


    【解决方案1】:

    总的来说,我使用:

    <%= f.select(:my_select_name, [1,2,3,4,5], {}, :style => "width:70px") %>
    

    或者,如果我使用 Twitter Bootstrap 之类的东西,我会使用一个类:

    <%= f.select(:my_select_name, [1,2,3,4,5], {}, :class => "col-md-1") %>
    

    【讨论】:

    • 谢谢,为我工作。你也可以这样做&lt;%= f.select(:foo_id, Foo.all.collect{|foo| [foo.name, foo.id,]}, {}, :style =&gt; "width:250px")
    【解决方案2】:

    select 的基本语法是

    select(object, method, choices, options = {}, html_options = {})
    

    选项被下拉选项值替换,并且 您可以将 html_options 替换为宽度。

    例如。 &lt;% select("company", "branch_id", Branch.all.collect {|b| [ b.name, b.id ] }, { :prompt =&gt; "Select" }, {:class =&gt; "companySelect" })

    对于 select_tag 你可以使用

    select_tag(name, option_tags = nil, options = {}) 
    

    这里的选项类似于select的html_options。

    例如。 &lt;%= select_tag 'company_name', options_for_select(get_company_name), :class =&gt; "select1"%&gt;

    更多详情请见select Tagselect

    【讨论】:

    • 嗨!我在哪里可以看到html_optionsoptions 的所有可用选项?
    • 常用的html_options有:multiple-允许多选,:disabled-禁用输入,:size-设置大小,:class-定义类,:onclick-调用javascript onclick等键创建标准的 HTML 属性。对于选项,您可以查看api.rubyonrails.org/classes/ActionView/Helpers/…
    • 谢谢,但我还是不明白如何设置选择框的大小。 :size 不适用于 select(仅适用于文本字段)。能详细点吗?
    • 如果您为选择框提供选项大小,它将把它作为垂直大小,对于宽度,您可以使用 " select1"%> 并为css中的select1类提供宽度,或者您可以使用 'width: 500px;'%>
    • 现在我明白了!非常感谢!!
    猜你喜欢
    • 2011-09-22
    • 1970-01-01
    • 2010-12-28
    • 2023-03-21
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多