[英]Display a form field according to the selected value in a drop-down list Angular TypeScript
[英]Display an input according to the value of a drop-down list symfony
我试图根据带有 symfony 的下拉列表的值显示输入
我的选择框
->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'],
]
])
当用户选择其他人时,它显示给他的另一个字段,我也在尝试使用此代码
表格类型
->add('otherspec', TextType::class, [
'attr' => ['class' => 'form-control mb-3'],
'label' => null,
])
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>
我的问题我的问题是当我选择选择框的任何值时,会出现第二个字段,但是我希望它在我选择 value="other" 时出现
谢谢
如果你 console.log 你的 event.target.value,你可以看到'1','2'... 违规者是: 'Select' => null
文件说:
如果选择值不是标量或字符串化表示不是唯一的,Symfony 将使用递增整数作为值。 当表单被提交时,具有正确类型的正确值将被分配给模型。
因此,如果要将字符串作为值,则必须在 Select 键中设置字符串。 或者你可以用 6 改变你的 js 条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.