简体   繁体   English

Yii小部件时间选择器-无法预先选择值

[英]Yii widget time picker - not able to pre-select the value

I have a time picker in my page. 我的页面上有一个时间选择器。 For this I used the following Yii widget. 为此,我使用了以下Yii小部件。 I couldn't able to populate the value dynamically . 我无法动态填充值。

$this->widget('bootstrap.widgets.TbTimePicker',
    array(
        'model' =>$Visits,
        'name' => "l_tme",
        'id'=>"l_time_id",
         'value' => $time_in_date,
        'noAppend' => true, 
        'options' => array(
            'disableFocus' => FALSE,
            'showMeridian' => TRUE 
        ),
        'htmlOptions'=> array('class'=>'form-control','style'=>'width:90px;','id'=>'l_tme_add'),
     )
);

Following Script code I have used. 以下是我使用过的脚本代码。

var s_val = '12:34:00 AM';
document.getElementById('l_time_id').value = s_val;

I have called the above script on page load. 我在页面加载时调用了上述脚本。

But the value is not populated in the field. 但是该值未填充在该字段中。 Please help me 请帮我

Try adding default value from PHP side: 尝试从PHP端添加默认值:

'htmlOptions' => [
    'value' => '12:34:00 AM',
    ...
]

Remove the id from htmlOptions and keep your widget code the same. htmlOptions删除id ,并保持相同的小部件代码。

'htmlOptions'=> array('class'=>'form-control','style'=>'width:90px;'),

After the document is loaded ie document.ready() , write the same code - 加载document.ready()后,即document.ready() ,编写相同的代码-

var s_val = '12:34:00 AM'; document.getElementById('l_time_id').value = s_val;

The value will be displayed in the timepicker fine. 该值将以时间选择器的精细显示。

It is incorrect to set the value of the time picker this way 这样设置时间选择器的值是不正确的

you have to follow doc at 您必须在以下位置阅读文档

http://jdewit.github.io/bootstrap-timepicker/ http://jdewit.github.io/bootstrap-timepicker/

$('#timepicker').timepicker('setTime', '12:45 AM'); $('#timepicker')。timepicker('setTime','12:45 AM');

also you have to be sure you are doing this after document is ready 您还必须确保在文档准备好之后执行此操作

Try setting the value in the model you are using for this form rather than in the widget itself. 尝试在用于此表单的模型中而不是在窗口小部件本身中设置值。

For example: 例如:

In your controller: 在您的控制器中:

$model = new YourActiveRecordModel();
$model->l_tme = date_format(new DateTime(),'Y-m-d H:i:s');

$this->render('your_view_file',array('model'=>$model));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM