好的,所以我有这个下拉菜单,您可以在其中选择多个值。现在假设我要编辑我的信息并创建一个包含多个选定值的下拉菜单。试图弄清楚它是怎么回事,但没有结果。
让我说我有:
$selected = array(3, 4);
$options = array(1,2,3,4);
echo $this->Form->select('Attendees', $options,array('multiple' => true, 'selected' => $selected));
我已使用此代码,但未选择任何内容。
答案 0 :(得分:5)
好的找到了一种方法,看起来需要像这样:
$selected = array(2, 3);
$options = array(1, 2, 3, 4);
echo $this->Form->input('Attendees', array('multiple' => true, 'options' => $options, 'selected' => $selected));
将输出:
$ selected检查每个元素的索引键而不是值本身。
答案 1 :(得分:0)
我在 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
以防万一有人没有注意到版本并坚持使用以前的解决方案,就像我一样:)