Symfony3 ChoiceType 中的 choice_attr 无法设置 id 属性的任何原因? [英] Any reason why the choice_attr in Symfony3 ChoiceType can't set the id attribute?

查看:31
本文介绍了Symfony3 ChoiceType 中的 choice_attr 无法设置 id 属性的任何原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的表单创建了一个带有以下 ChoiceType 元素的 Symfony3 表单类:

I have created a Symfony3 form class with the following ChoiceType element for my form:

->add('deg_certs', ChoiceType::class, array(
        'mapped' => false,
        'expanded' => true,
        'multiple' => true,
        'label' => 'Degrees/Certificates received:',
        'choices' => array(
                'BA/BS' => 'ba_bs',
                'AA/AS' => 'aa_as',
                'Voc. Cert.' => 'voc_cert',
                'Other' => 'other',
        ),
        'choice_attr' => function($val){
            return ['class' => $val];
        },
))

这工作正常并将类属性设置为选择值.但我想改为设置 id 属性.

This works fine and sets the class attribute to the choice value. But I want to instead set the id attribute.

我已经试过了:

'choice_attr' => function($val){
    return ['id' => $val];
},

我期待工作但没有.

我也在我的 Twig 模板中尝试过:

I also tried in my Twig template this:

{{ form_widget(form.deg_certs[3],
    {'attr':{'id':'deg_other',
    'onchange':'changeDegCertOther()',
    'style':'float:left'}}) }}

但这也没有设置 id 属性.注意:上面是尝试设置选项中第三项的id,以防您不熟悉.

But that doesn't set the id attribute either. Note: the above is trying to set the id of the 3rd item in the choices in case you are not familar.

这似乎是 Symfony3 代码不支持的问题.谁能确认一下?

This appears to be an issue with the Symfony3 code not supporting this. Can anyone confirm?

我想设置 id 属性的原因是因为在我的 Javascript 中我想使用 getElementById 而不是 getElementsByClassName(它获取一个数组).

The reason I want to set the id attribute, is because in my Javascript I want to use getElementById not getElementsByClassName (which gets an array).

推荐答案

我已经在 中解释过https://github.com/symfony/symfony/issues/20965 为什么会出现这种情况以及如何解决这个问题.

I've explained in https://github.com/symfony/symfony/issues/20965 why this is expected and how to work around that.

这篇关于Symfony3 ChoiceType 中的 choice_attr 无法设置 id 属性的任何原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆