【问题标题】:CakePHP 2.0 Select form Multiple selectedCakePHP 2.0 选择表单 多选
【发布时间】:2012-09-19 09:32:44
【问题描述】:

我有这个下拉菜单,您可以在其中选择多个值。现在假设我想编辑我的信息并制作一个包含多个选定值的下拉菜单。试图弄清楚它是怎么回事,但没有结果。

假设我有:

$selected = array(3, 4);
$options = array(1,2,3,4);

echo $this->Form->select('Attendees', $options,array('multiple' => true, 'selected' => $selected));

我使用过这段代码,但没有选择任何内容。

【问题讨论】:

    标签: php cakephp-2.0 selected form-helpers


    【解决方案1】:

    好的,找到了一个方法,看起来它需要是这样的:

    $selected = array(2, 3);
    $options = array(1, 2, 3, 4);
    
    echo $this->Form->input('Attendees', array('multiple' => true, 'options' => $options, 'selected' => $selected));
    

    将输出:

    • 1
    • 2
    • 3 次检查
    • 4 个检查

    $selected 检查每个元素的索引键而不是值本身。

    【讨论】:

    • @Jleagle 不,因为选定的将检查数组索引号或值。在这种情况下 [0] => 1, [1] => 2, [2] => 3, [3] => 4。因此选择了 [2] 和 [3]。
    • 哎呀,在我的脑海里我以为你已经这样做了:$options = array(1=>1, 2=>2, 3=>3, 4=>4);
    【解决方案2】:

    我在 cakePHP 3.9 中创建多选如下:

     echo $this->Form->input($tableName . '[' . $marker . ']', [
                    'type' => 'select',
                    'options' => $options,
                    'val' => $selected,
                    'multiple' => true,
                    'id' => $tableName . '-' . $num,
                ]);
    

    它使用 'val' 来预选,而不是 'selected': https://book.cakephp.org/3/en/views/helpers/form.html#creating-select-pickers

    以防万一有人没有注意到版本并被以前的解决方案卡住了,比如我:)

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多