1

如何使用 CakePHP 2 在选择(下拉)中获得类似于默认选项的占位符?

现在我有以下

<?php echo $this->Form->input('gender', array('options' => array('male' => 'Male', 'female' => 'Female'), 'empty' => '','label' => '','class'=>'scale')); ?>

我希望有一个灰色的默认值“性别”,以便用户真正知道下拉菜单的作用。我也不希望表单发送该值。

如果它是原始的 HTML,那么我猜它只是

selected="selected" disabled="disabled"
4

4 回答 4

3

它应该是。

$this->Form->input(
    'gender', 
    array(
        'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),           
        'empty' => 'Your placeholder will goes here',
        'label' => '',
        'class'=>'scale'
    )
);
于 2013-10-10T17:31:45.460 回答
2

我宁愿做这样的事情

$this->Form->input(
    'gender', 
    array(
        'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),           
        'empty' => '',
        'label' => '',
        'class'=>'scale'
    )
);
于 2013-10-10T08:02:54.253 回答
0

放置占位符的最简单方法:

echo $this->Form->input('User.role_id', array(
     'options' => $roles,
     'empty' => 'Choose', 
));
于 2014-04-17T13:09:05.653 回答
0

你在找empty,你可以把这个作为第三个参数传递给$this->Form->select()

array('empty' => array(0 => '-- Select --'))


文档

在此处查看文档:http: //book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs

/**
 * Method Signature
 *
 * public function select($fieldName, $options = array(), $attributes = array())
 *
 * ### Attributes:
 *
 * - `showParents` - If included in the array and set to true, an additional option element
 *   will be added for the parent of each option group. You can set an option with the same name
 *   and it's key will be used for the value of the option.
 * - `multiple` - show a multiple select box. If set to 'checkbox' multiple checkboxes will be
 *   created instead.
 * - `empty` - If true, the empty select option is shown. If a string,
 *   that string is displayed as the empty element.
 * - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
 * - `value` The selected value of the input.
 * - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
 * - `disabled` - Control the disabled attribute. When creating a select box, set to true to disable the
 *   select box. When creating checkboxes, `true` will disable all checkboxes. You can also set disabled
 *   to a list of values you want to disable when creating checkboxes.
 *
 */

PHP:

// Example..
$this->Form->select(
    'model', // First param = fieldName
    $options, // Second param = options
    array('empty' => array(0 => '-- Select --')) // Third param = attributes
); 

HTML:(渲染)

// Renders
<select>
    <option value="0">-- Select --</option>
</select>

如果您不需要选项中的值,则可以删除键并只传递一个字符串值。

于 2014-03-17T15:25:06.047 回答