2017-09-04 68 views
0

我symfony框架工作,我有兩個選擇類型:顯示隱藏的選擇類型使用javascript

第二選擇最初是隱藏的,並相對於第一價值選擇第二個選擇將顯示:對於我試圖做到這$:

腳本

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

FormType

$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') 
)) 

但第二選擇仍然沒有顯示,有人可以幫助我嗎?感謝所有

+0

你能分享你的HTML輸出? – SCC

+1

默認情況下,HiddenType通常不可見。將第二個選項設置爲TextType。 –

回答

1

更改soustypechoice類型(HiddenType場呈現爲<input type="hidden">),那麼你就可以隱藏或顯示在腳本領域。

FormType

... 
->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(); 
     } 
    }); 
}); 
+0

whene我試過你的解決方案第二選擇仍然顯示初始狀態,如果我從第一選擇中選擇「Unité」,則會添加其他選擇字段 –

+0

顯示錶單類型定義。 – miikes

+0

感謝@miikes它的工作,juste我有錯誤,我在同一頁中的其他腳本中修復它 –