简体   繁体   English

Tempus Dominus - 用javascript设置日期

[英]Tempus Dominus - setting date with javascript

I am using Tempus Dominus Bootstrap-4 datetime picker in a modal window that is shown when user double clicks on a specific row of a table: 我在一个模态窗口中使用Tempus Dominus Bootstrap-4日期时间选择器,当用户双击表格的特定行时显示该窗口:

$('#btn_modal').on('click', function() {
     // 
     // .... some irrelevenat code, like geting data of a specific row, 
     // .... filling other fields
     //
     $('#modal_datetimepicker').datetimepicker({
        defaultDate: moment(row['date_rkw'], "YYYY-MM-DD"),
        viewMode: 'days',
        format: 'YYYY-MM-DD',
        extraformats: [ 'YYYY-MM-DD' ]
     });
     $('#modal').modal();
});

The date is properly set first time the modal is shown, but next time the modal is shown (selecting other row to edit), it keeps the previous value, completely ignoring the above call with new value to defaultDate. 在第一次显示模态时正确设置日期,但下次显示模态(选择要编辑的其他行)时,它会保留前一个值,完全忽略上面的调用,并使用defaultDate的新值。

What is the proper way to set datetimepicker to a specific date? 将datetimepicker设置为特定日期的正确方法是什么? I tried calling destroy with 我试着用破坏叫

$('#modal_datetimepicker').datetimepicker('destroy')

or even 甚至

$('#modal_datetimepicker').val(row['date_rkw'])

but with no luck. 但没有运气。

You can try destroying datepicker when modal is hidden 隐藏modal时,您可以尝试销毁datepicker

$('#modal').modal('show')
.on('hidden.bs.modal',function(){
  $('#modal_datetimepicker').datetimepicker('destroy')  
});

Have a look at this reference - https://getbootstrap.com/docs/4.0/components/modal/ 看看这个参考 - https://getbootstrap.com/docs/4.0/components/modal/

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

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