选择当前日期后,bootstrap-datepicker清空字段 [英] bootstrap-datepicker empties field after selecting current date

查看:707
本文介绍了选择当前日期后,bootstrap-datepicker清空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我启用了自动关闭,并且我从已选择的日历中选择了该字段,则会清空该字段,然后按预期关闭日期选择器。我怎么还有autoclose功能,但没有把它清空?

If I have autoclose enabled, and I select the field from the calendar that is already chosen, it empties the field and then closes the datepicker as expected. How can I still have the autoclose feature, but not have it empty the field?

看一个例子的演示, http://eternicode.github.io/bootstrap-datepicker/?markup=input&format=& ; weekStart =&放大器;的startDate =安培;结束日期=安培; startView = 0&安培; minViewMode = 0&安培; todayBtn =假放,语言= EN&放大器;定向=汽车和放大器; multidate =安培; multidateSeparator =&放大器;自动关闭= ON&放大器; keyboardNavigation = ON&安培; forceParse = on#sandbox


  1. 确保autoclose已开启

  2. 选择日期。

  3. 再次打开日期选择器,再次选择当前选择的日期。

  4. 字段再次为空

  1. Make sure autoclose is on
  2. Select a date.
  3. open the datepicker again, select the currently selected date again.
  4. field is empty again

谢谢

推荐答案

Bootstrap Datepicker提供事件来实现目标。

Bootstrap Datepicker provides events that you can leverage to accomplish your goal.

这是一种方法:

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

$('#sandbox-container input').on('show', function(e){
    if ( e.date ) {
         $(this).data('stickyDate', e.date);
    }
    else {
         $(this).data('stickyDate', null);
    }
});

$('#sandbox-container input').on('hide', function(e){
    var stickyDate = $(this).data('stickyDate');

    if ( !e.date && stickyDate ) {
        $(this).datepicker('setDate', stickyDate);
        $(this).data('stickyDate', null);
    }
});

不一定是最优雅的,但正如你在这里看到的,它可以解决问题:

Not necessarily the most elegant, but as you can see here, it does the trick:

http://jsfiddle.net/klenwell/LcqM7/(在Chrome中测试)

http://jsfiddle.net/klenwell/LcqM7/ (tested in Chrome)

这篇关于选择当前日期后,bootstrap-datepicker清空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆