Joomla 3.2 分组列表自定义字段列表没有 SELECTED 值

2022-01-06 00:00:00 php joomla joomla3.0

我正在尝试按照此页面的说明为 Joomla 3 的模板参数创建自定义字段表单创建自定义表单字段类型

I am trying to create a custom field form for template parameter for Joomla 3, by following instruction from this page Creating a custom form field type

这是我的代码:

class JFormFieldMy extends JFormField {
protected $type = 'my';
public function getInput() {
                return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                        '<optgroup label="First">'.
                            '<option value="1">One</option>'.
                            '<option value="2">Two</option>'.
                            '<option value="3">Three</option>'.
                        '</optgroup>'.
                        '<optgroup label="Second">'.
                            '<option value="4">Four</option>'.
                            '<option value="5">Five</option>'.
                            '<option value="6">Six</option>'.
                        '</optgroup>'.
                       '</select>';
        }
} 

效果很好,值已保存,但所选值没有 selected="selected" 状态,因此下拉列表将始终在我选择时显示选项一"/实际值为二"

It works good, the value is saved, but the selected value doesn't have the selected="selected" state so the dropdown list will always show the option 'One' when I choose / the actual value is 'Two'

我已阅读此解决方案:Joomla 2.5 自定义字段列表未在显示中选择,但这是针对通用列表类型,而不是我想要的分组列表.

I have read this solution : Joomla 2.5 Custom Field List not SELECTED in display but that's for generic list type not for grouped list I wanted.

谁能帮帮我?谢谢

推荐答案

您没有设置列表的选定元素:

You are not setting the selected element of the list:

<option value="the_value" selected>....</option>

另一种方法:不是从 JFormField 派生您的类,您应该从抽象类 JHtmlList 派生它(您可以在 libraries/cms/html/list.php 上找到它)你可以开始以 libraries/cms/form/field/limitbox.php 为例.

Another approach: instead of deriving your class from JFormField you should derive it from the abstract class JHtmlList (you will find it on libraries/cms/html/list.php) You may start taking libraries/cms/form/field/limitbox.php as an example.

相关文章