问题描述
我的编辑表单中有多个选择输入:-
I have a multiple select input in my edit form:-
<?php echo $this->Form->input('Article.tags', array('type' => 'select', 'multiple' => true, 'options' => $tags, 'selected' => array($selected))); ?>
回显后,选择 $ 变量如下所示:-
When echo-ed, the $selected variable will look like this:-
"MySQL", "PHP"
但是,输入不会自动选择与标签匹配的选项。
However, the input does not automatically select the option that matches the tag.
但是,当我手动输入所选选项时,它将自动选择该两个选项;例如
However, when I manually put in the selected option, it will automatically select that two option; e.g.
<?php echo $this->Form->input('Article.tags', array('type' => 'select', 'multiple' => true, 'options' => $tags, 'selected' => array("MySQL", "PHP"))); ?>
是否可以解决此问题?谢谢。
Is there anyway to fix this? Thanks.
推荐答案
啊,我已经解决了这个问题。
Ahh, I got this fixed.
我看错了方向。我假设所选值应为带引号和逗号分隔的字符串形式。 (例如 MySQL, PHP, jQuery)。
I was looking at the wrong direction. I assumed that the selected values should be in string form with quotes and comma separated. (e.g. "MySQL", "PHP", "jQuery").
相反,它应为数组格式,不带引号和逗号。例如
Instead, it should be in array format without any quote and comma. e.g.
Array ( [0] => MySQL [1] => PHP [2] => jQuery )
我整理好了数组,然后将其传递给视图;例如$ this-> set('selected',$ myArray);
Once I've got the array sorted out I pass it into view; e.g. $this->set('selected', $myArray);
然后在表单上,我只需要像这样回显它:-
Then on the form, I would just have to echo it out like this:-
<?php echo $this->Form->input('Article.tags', array('type' => 'select', 'multiple' => true, 'options' => $tags, 'selected' => $selected)); ?>
这篇关于CakePHP多次选择“已选择”不管用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!