簡體   English   中英

根據下拉列表 symfony 的值顯示輸入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM