0

我需要在我的下拉列表中添加水平线。我研究并发现了这种方式:

<select>
    <option>First</option>
    <option disabled>──────────</option>
    <option>Second</option>
    <option>Third</option>
</select>

问题是我使用 Codeigniter form_dropdown() 并且无法在我的代码中插入行。你能帮我在下面的代码中插入水平线吗?

$options = array(
                  ''        => 'Select Size',
                  ''        => '-----------', //does not work
                  'small'   => 'Small Shirt',
                  'med'     => 'Medium Shirt',
                  ''        => '-----------', // does not work
                  'large'   => 'Large Shirt',
                  'xlarge'  => 'Extra Large Shirt',
                );
echo form_dropdown('shirts', $options, 'set_value('shirts')');
4

1 回答 1

1

检查你的语法。我认为当您在实际表单元素中添加单引号和双引号时。此外,您的选项数组中的最后一项不需要尾随,

否则,您的代码看起来“不错”。

php

$options = array(
    '' => 'Select Size',
    '-----------',
    'small' => 'Small',
    'medium' => 'Medium',
    '-----------',
    'large' => 'Large',
    'xlarge' => 'Extra Large'
);

echo form_dropdown('shirts', $options, $this->input->post('shirts'));

编辑

要创建下拉菜单以使用选项组:“如果作为 $options 传递的数组是多维数组,form_dropdown() 将生成一个以数组键作为标签的数组。”

$options = array(
    '' => 'Select Size',
    'Children' => array(
        'small' => 'Small',
        'medium' => 'Medium'
    ),
    'Adults' => array(
        'large' => 'Large',
        'xlarge' => 'Extra Large'
    )
);

echo form_dropdown( 'shirts', $options, $this->input->post( 'shirts'));

但我发现,您的 optgroup 标签必须是唯一的。"Children"/"Adults" 否则只会渲染最后一组。因此,您可能会遇到需要将数据设置为“子大”而不仅仅是“大”的情况。

如果您想在使用 时使​​用禁用的选项form_dropdown,您可能需要扩展表单助手库并构建自己的。否则,您可以只使用普通的旧 HTML 语法。然后你可以disabled="disabled"在选项上添加右边。

希望这可以帮助...

于 2015-01-12T16:53:05.197 回答