简体   繁体   English

根据下拉列表 symfony 的值显示输入

[英]Display an input according to the value of a drop-down list symfony

im trying to display an input according to the value of a drop-down list with symfony我试图根据带有 symfony 的下拉列表的值显示输入

my select box我的选择框

->add('specialite', ChoiceType::class, [
                'attr' => ['class' => 'form-control mb-3'],
                'label' => 'Speciality',
                'choices' => array(
                    'Select' => null,
                    'Nurses' => 'nurses',
                    'Doctors' => 'Doctors',
                    'Engineers' => 'Engineers',
                    'IT-Specialist' => 'IT-Specialist',
                    'Anesthetist technicians' => 'Anesthetist technicians',
                    'Others' => 'Others',
                ),
                'choice_attr' => [
                    'Select' => ['disabled'=>'disabled'],
                ]
                
            ])

when the user select others , another field it displayed to him , im trying also with this code当用户选择其他人时,它显示给他的另一个字段,我也在尝试使用此代码

form type表格类型

    ->add('otherspec', TextType::class, [
        'attr' => ['class' => 'form-control mb-3'],
        'label' => null,
    ])

html.twig html.twig

<script>
let foo = document.getElementById("emplopyer_specialite");
let bar = document.getElementById("emplopyer_otherspec");

foo.addEventListener('change', (event) => {
    if (event.target.value === 'Others') {
          bar.style.display = 'none'; // Hide the element.

  } else {
        bar.style.display = 'inline'; // Show the element.

  }
});

</script>

My problem my problem is whene i select any value of the select box , the second field appear , however i want it appear when i select the value="other"我的问题我的问题是当我选择选择框的任何值时,会出现第二个字段,但是我希望它在我选择 value="other" 时出现

thanks谢谢

If you console.log your event.target.value, you can see '1', '2'... The offender is : 'Select' => null如果你 console.log 你的 event.target.value,你可以看到'1','2'... 违规者是: 'Select' => null

Documentations says :文件说:

If there are choice values that are not scalar or the stringified representation is not unique Symfony will use incrementing integers as values.如果选择值不是标量或字符串化表示不是唯一的,Symfony 将使用递增整数作为值。 When the form gets submitted the correct values with the correct types will be assigned to the model.当表单被提交时,具有正确类型的正确值将被分配给模型。

So, if you want string as value, you have to set string in your Select key.因此,如果要将字符串作为值,则必须在 Select 键中设置字符串。 Or you can change your js condition with 6.或者你可以用 6 改变你的 js 条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据下拉列表中选择的值显示表单域 Angular TypeScript - Display a form field according to the selected value in a drop-down list Angular TypeScript 对于从下拉框中选择的特定值,在下拉列表中显示其对应的值? - For a particular selected value from a drop-down box, Display its corresponding values in drop down list? 根据在另一个下拉列表中所做的选择填充一个下拉列表 - Populating a drop-down list according to the selection made in another drop-down list 如何根据第一个下拉列表过滤第二个下拉列表 - how to filter 2nd drop-down list according to 1st drop-down list angularjs根据其他下拉列表中的选定值过滤下拉列表 - angularjs filter drop-down list according to selected values in other drop-down lists 使用Javascript获取下拉列表的值 - Getting Value of Drop-Down List with Javascript 如何在下拉列表中设置值 - How to set a value in a drop-down list 如何使用取决于另一个下拉列表中所选选项的下拉列表显示表格简码 - how to display table shortcode with a drop-down list that depends on the selected option in another drop-down list 根据另一个下拉列表值更改下拉列表值 - Change drop-down list value based on another drop-down list value 角度显示基于上下拉列表值的一个下拉列表 - Angular show one drop-down list based on upper drop-down list value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM