【问题标题】:make specific value selected in dropdown fields cakephp 3.0在下拉字段中选择特定值 cakephp 3.0
【发布时间】:2016-07-06 11:57:25
【问题描述】:

在选择下拉列表中,从下拉列表 Cakephp3.0 中的值中选择一个特定值。我正在使用以下代码:

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

国家名称出现在下拉列表中,但我想选择设置为变量 $abc(即印度)的特定值。

【问题讨论】:

标签: php cakephp-3.0


【解决方案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'));

【讨论】:

    【解决方案2】:

    使用form input helper 选项。像这样:

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

    建议 - 阅读文档一次,然后开始开发。你永远不会被这么简单的问题困住。遇到任何问题时,请先参考文档。

    【讨论】:

      【解决方案3】:
      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'
      ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-21
        • 2013-02-16
        • 2014-10-03
        • 2013-05-13
        • 1970-01-01
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多