bootstrap datetimepicker需要有条件的添加keyBinds

bootstrap datetimepicker need to add keyBinds conditionally

我需要添加 keyBinds 用于有条件地删除和输入按钮属性。 我尝试使用 Object.assign() 方法,但不确定如何获取与我的输入控件关联的 datetimepicker 对象,以及如何有条件地启用或禁用 [​​=13=]。

var $inputCtrl = $('.inputCtrl');
$inputCtrl.datetimepicker({
    locale: 'en-US',
    format: 'DD-MM-YYYY',
    useCurrent: false,
    keepInvalid: true
});

Object.assign($inputCtrl.data("DateTimePicker"), bConditionalDeleteTest && {keyBinds: {'delete': null}});

如果我们静态初始化这个对象,它将是

$('.inputCtrl').datetimepicker({
    locale: 'en-US',
    format: 'DD-MM-YYYY',
    useCurrent: false,
    keepInvalid: true,
    keyBinds: {'delete': null} /*this would disable the default Delete key behavior of the control.*/
});

你可以简单地使用keyBinds功能。

keyBinds()

Returns a string variable with the currently set options.keyBinds option.

keyBinds(object)

Takes an object value.

Allows for several keyBinding functions to be specified for ease of access or accessibility. See the options page for defaults.

你可以有这样的东西:

var $inputCtrl = $('.inputCtrl');
$inputCtrl.datetimepicker({
    locale: 'en-US',
    format: 'DD-MM-YYYY',
    useCurrent: false,
    keepInvalid: true
});

if( bConditionalDeleteTest ){
  $inputCtrl.data("DateTimePicker").keyBinds({'delete': null});
}