2016-07-06 94 views
0

在select下拉菜單中,從下拉列表CakePHP3.0中的值中選擇一個特定值。我使用下面的代碼:在下拉字段中選擇特定值cakephp 3.0

$abc = 'india'; 
    echo $this->Form->input('location_id', 
['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false]); 

國家的名稱在下拉列表中來,但我想要選擇特定的值被設置爲變量$ ABC(即印度)。

+0

歡迎堆棧溢出:-) 請看[問] 以及如何創建一個[MCVE。這將有助於獲得有用的答案。你的問題很難閱讀 – JimHawkins

回答

1

試試這個代碼:

使用 '默認' 鍵

$abc = array('1' => 'One','2'=> 'Two'); 
echo $this->Form->input('location_id', 
          'options' => $abc, 
          default' =>$abc[1], 
          ['empty' =>'Please select', 
          'required'=>'required', 
          'class' => 'form-control', 
          'label'=>false] 
         ); 

其中$ ABC [0]是你想要的選擇項目的關鍵

是這樣的:

$options = array('M' => 'Male', 'F' => 'Female'); 
echo $this->Form->select('field', $options, array('default' => 'F')); 
0

使用選項form input helper。像這樣:

$selected = 'india'; 
echo $this->Form->input('location_id', ['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false, 'value'=> $selected]); 

建議 - 閱讀文檔一次,然後開始開發。你永遠不會陷入這樣簡單的問題。無論什麼時候遇到任何問題,請先參閱文檔。

0
echo $form->input('field_name', array(
      'type' => 'select', 
    'options' => $arrayOfOptions, // typically set from $this->find('list') in controller 
    'label'=> 'Label name here', 
    'value' => $arrProjectLeaderDetails['id'], // specify default value 
    'escape' => false, // prevent HTML being automatically escaped 
    'error' => false, 
    'class' => 'input_select_medium' 
)); 
相關問題