【发布时间】:2019-06-10 10:09:58
【问题描述】:
我尝试在我的 CakePHP 3.6.14 应用程序中实现 this example。
(function( $ ) {
$.widget( "ui.dp", {
_create: function() {
var el = this.element.hide();
this.options.altField = el;
var input = this.input = $('<input>').insertBefore( el )
input.focusout(function(){
if(input.val() == ''){
el.val('');
}
});
input.datepicker(this.options)
if(convertDate(el.val()) != null){
this.input.datepicker('setDate', convertDate(el.val()));
}
},
destroy: function() {
this.input.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
var convertDate = function(date){
if(typeof(date) != 'undefined' && date != null && date != ''){
return new Date(date);
} else {
return null;
}
} })( jQuery );
所以在default.ctp我有这个代码:
$(document).ready(function() {
$( "input.datepicker" ).dp({
dateFormat: 'dd/mm/yy',
altFormat: 'yy-mm-dd'
});
});
在edit.ctp我有这个控制:
echo $this->Form->control('created_date', ['class' => 'datepicker', 'type' => 'text', 'empty' => true]);
虽然它在数据库中正确提交并存储了日期,但当我再次打开编辑页面时,我看到的是今天的日期,而不是数据库中的日期。数据库值为:2019-06-27,如果我将dateFormat 选项更改为yy-mm-dd,我仍然会得到今天的日期,而不是保存在数据库中的日期。
但是在生成的html代码中我可以看到值是正确的:
【问题讨论】:
-
如果不使用日期选择器,并且不覆盖表单控件中的默认日期格式,保存的日期是否正确显示?
标签: jquery cakephp datepicker jquery-ui-datepicker