0

我希望在 ctp 时间下拉列表中显示默认时间为上午 9:00。以下是我的 ctp 代码:

<?php
                                      echo $this->Form->input('Rideoffer.DepartureTime', array(
                                                        'type' => 'time',

                                                         'interval' => 5
                                                         ));
                             ?>

我怎么做?

4

2 回答 2

1

使用“选定”选项

<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
    'type' => 'time',
    'interval' => 5,
    'selected' => '09:00:00',
));
?>
于 2013-03-11T07:48:58.033 回答
0

设置默认数据的最佳方法是使用控制器(并且仅在未发布时):

if (!$this->request->is('post')) {
    $this->request->data['Rideoffer']['DepartureTime'] = '09:00:00';
}

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#common-options

这适用于所有表单元素。

表单的“selected”、“value”、“checked”和其他硬编码属性通常会在发布不成功后直接破坏表单(如果表单包含验证错误):它会丢失所有输入的数据并将它们重置为原来的值在此之前对于前端用户来说通常很烦人。

http://www.dereuromark.de/2010/06/23/working-with-forms/

于 2013-03-11T11:19:39.027 回答