jQuery Bootstrap 日期选择器在选择后隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21151830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:27:18  来源:igfitidea点击:

Bootstrap datepicker hide after selection

jquerytwitter-bootstrapdatepicker

提问by jheul

How do I hide the calendar after a date is selected? Is there a specific function that I can use? My code below:

选择日期后如何隐藏日历?有我可以使用的特定功能吗?我的代码如下:

$('#dp1').datepicker({
    format: 'mm-dd-yyyy',
    startDate: '-15d',
    autoclose: true,
    endDate: '+0d' // there's no convenient "right now" notation yet
});

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Felix

You can use event changedate()to keep track of when the date is changed together with datepicker('hide')method to hide the datepickerafter making selection:

您可以使用 eventchangedate()来跟踪日期何时更改以及datepicker('hide')隐藏datepicker选择后的方法:

$('yourpickerid').on('changeDate', function(ev){
    $(this).datepicker('hide');
});

Demo

演示

UPDATE

更新

This was the bug with autoclose: true. This bug was fixed in latest master. SEE THE COMMIT. Get the latest code from GitHub

这是autoclose: true. 此错误已在最新的 master 中修复。见COMMIT。获取最新代码GitHub

回答by Ola

If it's any help to anyone, the Version 2.0 of the bootstrap datepicker no longer works with the accepted answer.

如果对任何人有帮助,引导日期选择器的 2.0 版不再适用于已接受的答案。

Here's how I got it working on mine:

这是我如何让它在我的工作:

$('yourpickerid').datepicker({
    format: 'dd/mm/yyyy',
}).on('changeDate', function(e){
    $(this).datepicker('hide');
});

See http://bootstrap-datepicker.readthedocs.org/en/latest/events.html#changedate

请参阅http://bootstrap-datepicker.readthedocs.org/en/latest/events.html#changedate

回答by MaxEcho

$('#input').datepicker({autoclose:true});

回答by user3674664

If you're looking to override the behavior of the calendar in general, globally, try editing the Datepicker function (in my example it was line 82),

如果您希望在全局范围内覆盖日历的一般行为,请尝试编辑 Datepicker 函数(在我的示例中为第 82 行),

from

    this.autoclose = false;

to

    this.autoclose = true;

Worked fine for me, as I wanted to have all my calendar instances behave the same.

对我来说效果很好,因为我希望所有日历实例的行为都相同。

回答by papacho

The problem can be stopped, blocking hide event for input element by this linese:

可以通过以下方式停止问题,阻止输入元素的隐藏事件:

var your_options = { ... };
$('.datetimepicker').datetimepicker(your_options).on('hide', function (e) {
    e.preventDefault();
    e.stopPropagation();
});

回答by user3615318

  1. Simply open the bootstrap-datepicker.js
  2. find : var defaults = $.fn.datepicker.defaults
  3. set autoclose: true
  1. 只需打开 bootstrap-datepicker.js
  2. 找 : var defaults = $.fn.datepicker.defaults
  3. autoclose: true

Save and refresh your project and this should do.

保存并刷新您的项目,这应该可以。

回答by Ankit24007

In bootstrap 4 use "autoHide : true"

在引导程序 4 中使用“autoHide:true”

$('#datepicker1').datepicker({
    autoHide: true,
    format: 'mm-yyyy',
    endDate: new Date()
});

回答by Gosha

Close datetimepicker when date select(datetimepicker show date with time)

选择日期时关闭日期时间选择器(日期时间选择器显示带时间的日期)

$('.datepicker').datepicker({
    autoclose: true,
    closeOnDateSelect: true
}); 

回答by mirmo

$('yourpickerid').datetimepicker({
           pickTime: false
}).on('changeDate', function (e) {
           $(this).datetimepicker('hide');
});

回答by Vladimir Kulakov

You can change source code, bootstrap-datepicker.js. Add this.hide();like ne

您可以更改源代码bootstrap-datepicker.js。添加this.hide();喜欢 ne

  if (this.viewMode !== 0) {
    this.date = new Date(this.viewDate);
    this.element.trigger({
        type: 'changeDate',
        date: this.date,
        viewMode: DPGlobal.modes[this.viewMode].clsName
    });
    this.hide();//here
 }