本文介绍了Ruby on rails下拉列表< select>菜单 - 如何风格或应用类的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ruby on rails 3上创建颜色下拉菜单?



我知道如何在html,css和javascript中做到如下,但我不知道



理想情况下使用collection_select或collection_options_for_select创建< select> s和< options> 。使用这些将更喜欢手动循环通过收集和吐出的碎片和样式的每件,虽然可以这样做。



代码部分我想要获得的是:

 < select> 
< option value =>突出显示< / option>
< option value =#000000style =background-color:Black; color:#FFFFFF;> Black< / option>
< option value =#808080style =background-color:Gray;> Gray< / option>
< option value =#A9A9A9style =background-color:DarkGray;> DarkGray< / option>

完整代码为:

options_for_select 绝对允许您为个别选项提供样式信息。



这直接来自 options_for_select

的文档

  options_for_select([Denmark,[USA,{:class =>'bold'}],Sweden],[USA,Sweden])



提供

 < option value = Denmark>丹麦< / option> \ n< option value =USAclass =boldselected =selected> selected> Sweden< / option> 

  options_for_select([[Dollar,$,{:class =>bold}],[Kroner,DKK,{:onclick =>alert ;}]])

给出

 < option value =$class =bold> Dollar< / option> \\\
< option value =DKKonclick =alert('HI'); > Kroner< / option>

在您的情况下会是

  options_for_select([[Black,{:style =>background-color:Black; color:#ffffff}],
[Gray,{ style =>background-color:Gray}],
[DarkGray,{:style =>backgorund-color:DarkGray}]])


How can I create a colors dropdown menu in ruby on rails 3?

I know how to do it in html, css and javascript as following but i dont know how in ruby on rails 3.

Ideally using collection_select or collection_options_for_select other collection objects that build <select>'s and <options>'s. Using these would be preferred to manually looping through the collection and spitting out pieces and styling each piece though that could be done.

Code partial of what I am trying to get is:

<select>
<option value="">Highlight</option>
<option value="#000000" style="background-color: Black;color: #FFFFFF;">Black</option>
<option value="#808080" style="background-color: Gray;">Gray</option>
<option value="#A9A9A9" style="background-color: DarkGray;">DarkGray</option>

Full Code is at:http://pietschsoft.com/post/2004/09/20/Color-the-background-of-items-in-a-Dropdown-box-in-your-HTML-pages.aspx

解决方案

Rails Helper options_for_select absolutely allows you to provide styling information for individual options.

This is straight from the documentation of options_for_select

  options_for_select([ "Denmark", ["USA", {:class=>'bold'}], "Sweden" ], ["USA", "Sweden"])

gives

  <option value="Denmark">Denmark</option>\n<option value="USA" class="bold" selected="selected">USA</option>\n<option value="Sweden" selected="selected">Sweden</option>

and

  options_for_select([["Dollar", "$", {:class=>"bold"}], ["Kroner", "DKK", {:onclick => "alert('HI');"}]])

gives

  <option value="$" class="bold">Dollar</option>\n<option value="DKK" onclick="alert('HI');">Kroner</option>

And in your case it would be

options_for_select([["Black", {:style => "background-color: Black; color: #ffffff"}],
                    ["Gray", {:style => "background-color: Gray"}],
                    ["DarkGray", {:style => "backgorund-color: DarkGray"}]])

这篇关于Ruby on rails下拉列表&lt; select&gt;菜单 - 如何风格或应用类的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:48