2016-07-06 92 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' 
)); 
相关问题