這裏是我想出了:
function date_time_picker_field_report($field, format)
{
var id_of_field = $field.attr('id');
var name_of_field = $field.attr('name');
var id_copy_of_field = id_of_field+'_copy';
var name_copy_of_field = name_of_field+'_copy';
//Set $field to be a copy that is used for actual display of date info
$field.attr('id', id_copy_of_field);
$field.attr('name', name_copy_of_field);
//create hidden input to track field behind the scenes (better value)
$('<input>').attr({
type: 'hidden',
id: id_of_field,
name: name_of_field
}).insertAfter($field);
$field.on("dp.change", function(e)
{
//If we have a space seperator it has a time; otherwise just a date (time has 2 spaces; between date + time and AM/PM (12 hour))
var does_date_have_time = format.indexOf(' ') != -1;
var date = e.date;
var formated_date = null;
if (does_date_have_time)
{
formated_date = date.format("YYYY-MM-DD HH:mm");
}
else
{
formated_date = date.format("YYYY-MM-DD");
}
$('#'+id_of_field).val(formated_date);
});
var defaultDate = null;
if (id_of_field == 'start_date')
{
defaultDate = moment();
defaultDate.set('hour', 0);
defaultDate.set('minute', 0);
}
else if(id_of_field == 'end_date')
{
defaultDate = moment();
defaultDate.set('hour', 23);
defaultDate.set('minute', 59);
}
$field.datetimepicker({format: format, locale: LOCALE, defaultDate: defaultDate});
}
您是否嘗試過更改事件? – Sushil
@Sushil我沒有;但我不確定這會起作用,因爲該字段可以具有默認值,並且可能永遠不會更改。我想我可以有一個負載事件以及更改。我很驚訝更多的人沒有遇到過這種情況。 –
是的我想更多的人不使用這個或他們可能沒有遇到這種情況。但我認爲更改函數應該是你應該定位的地方,否則你必須按照你提到的形式提交代碼。 – Sushil