3

如何向我的选择菜单选项标签添加其他属性?像这样:

<select class="test" name="data[Test][test]">
    <option value="1" data-price="100">My Option</option>
</select>

如何添加data-price="100"

我尝试了这样的事情,但没有奏效:

<?php
    echo $this->Form->select('test', $options, null, array(
        'class' => 'test',
        'options' => array(
            'data-price' => 100
        )
    ));
?>
4

5 回答 5

4

看看这个:http: //www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/

“为某些选择选项设置附加属性”

于 2012-08-09T11:56:01.860 回答
3
you can try this
    echo $this->Form->input('test', array(
                        'options' => array(
                                            1=>array(
                                            'data-price' => 100, 
                                            'value' => '1', 
                                            'name' => 'My Option'
                                        )),'class' => 'test')
                                    );
于 2012-08-14T20:55:54.007 回答
2

你可以这样做:

$options = array(
    ...
    array('name' => 'United states', 'value' => 'USA', 'title' => 'the title that you want', 'class' => 'something'),
    array('name' => 'USA', 'value' => 'USA', 'title' => 'the other title that you want', 'class' => 'otherthing'),
 );

 echo $this->Form->input('test', array('type'=>'select', 'options'=>$options));
于 2013-03-13T05:06:51.060 回答
0

您必须手动构建选择 HTML

你也可以参考How to give select tag an attribute in cake php?

于 2012-08-09T11:52:08.557 回答
0

在 cakephp 4

寻找文件

控制器

$countries = $this->Countries->find('all')->where([
            'active' => 1
        ]);

$options = $examples->map(function ($value, $key) {
    return [
        'value' => $value->id,
        'text' => $value->name,
        'data-flag' => $value->iso_code
    ];
});

在视图/模板中

<?= $this->Form->control('country_residence_id', [
                'label' => false,
                'options' => $countries,
                'class' => 'form-control select2-flag-search',
                'data-placeholde' => __('Select Country')
            ]) ?>
于 2021-11-26T14:50:27.297 回答