【问题标题】:symfony2 customize form select optionsymfony2 自定义表单选择选项
【发布时间】:2012-06-09 15:33:03
【问题描述】:

我正在尝试做一个简单的表单来添加一个带有名称和颜色的活动。

所以我想制作一个包含一些颜色数组的列表,现在它正在工作我有颜色的名称,但我想要的是制作类似的东西:

<select id="a" name="select" style="width: 10%;">
    <option style="background-color: FF0;" value="FF0"></option>
    <option style="background-color: F0F;" value="F0F"></option>
    <option style="background-color: 0FF;" value="0FF"></option>
</select> 

如何添加 "style="background-color:"mycolor;"到我的表格?

public function buildForm(FormBuilder $builder, array $options)
{        
    $builder->add('name');         
    $listColor;

    for($r = 0;$r < 255; $r = $r + 120)
    {
        for($g = 0;$g < 255; $g = $g + 120)
        {    
            for($b = 0;$b < 255; $b = $b + 120)        
            {
                $string = dechex($r).dechex($g).dechex($b);
                $listColor[$string] = $string;
            }
        }
    }
    $builder->add('color', 'choice', array(
        'choices' => $listColor));
}

在数组中有一些选项有诀窍吗?放一些html?

我发现的最接近的事情是在你的表单上添加一个类名,以便在它之后在 css 中做一些事情......

{{ form_widget(form, { 'attr': {'class': 'myclass'}}) }}

感谢 ;)

拉夫

【问题讨论】:

    标签: html css forms symfony customization


    【解决方案1】:

    你可以使用jquery来解决这个问题,试试这个例子:

    JSBIN "style="background-color:"mycolor;"

    $(document).ready(function(){
        $("#a option").each(function(index,value){
           var option = $(this);
           var bc= 'background-color:'+option.val()+';';
           option.attr('style',bc);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2019-10-13
      • 2012-12-27
      • 2012-01-20
      • 1970-01-01
      • 2012-12-29
      相关资源
      最近更新 更多