CakePHP 1.3 -添加额外的属性来选择菜单选项

vfh0ocws  于 2022-11-24  发布在  PHP
关注(0)|答案(5)|浏览(155)

如何将其他属性添加到选择菜单选项标记中?例如:

<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
        )
    ));
?>
bwleehnv

bwleehnv2#

在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')
            ]) ?>
cld4siwp

cld4siwp4#

you can try this
    echo $this->Form->input('test', array(
                        'options' => array(
                                            1=>array(
                                            'data-price' => 100, 
                                            'value' => '1', 
                                            'name' => 'My Option'
                                        )),'class' => 'test')
                                    );
eblbsuwk

eblbsuwk5#

您可以这样做:

$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));

相关问题