使用 javascript 显示隐藏的 select 类型

show hidden select type with javascript

我使用 symfony 框架,我有两个 select 类型:

第二个 select 最初是隐藏的,相对于第一个 select 的值,将显示第二个 select :为此我尝试这样做 $this :

脚本

<script type="text/javascript">
$(document).ready(function () {
    $('.type-produit ').change(function() {
     if ($('select[id$="_type"]>option:selected').text() == "Unité")
     {  $('.hidden ').show();  } 
    });
}); 
</script>

表格类型

$formMapper
->add('type','choice', array(
     'choices' => array('Kg' => 'Kg', 'Unité' => 'Unité'),
     'label' => 'Type de vente',
     'attr' =>array('class'=>'type-produit')
))

->add('soustype',HiddenType::class, array(
     'data' => ['Liquide'=>'Liquide','Autres'=>'Autres'],
     'label' => 'Sous types',
     'attr'=>array('class'=>'hidden')
))

但是第二个select还是没有显示,有人可以帮帮我吗?感谢大家

soustype字段更改为choice类型(HiddenType字段呈现为<input type="hidden">),然后您可以在脚本中隐藏或显示该字段。

表格类型

...
->add('soustype', 'choice', array(
        'choices' => ['Liquide'=>'Liquide','Autres'=>'Autres'],
        'label' => 'Sous types',
        'attr' => array('class'=>'soustype')
))

脚本

$(document).ready(function () {
    $('.soustype').hide();

    $('.type-produit ').change(function() {
         if ($('select[id$="_type"]>option:selected').text() == "Unité") { 
            $('.soustype ').show();  
        }
    });
});