【问题标题】:Prevent jQuery UI Datepicker (inline type) from selecting today's date防止 jQuery UI Datepicker(内联类型)选择今天的日期
【发布时间】:2018-05-15 11:47:14
【问题描述】:

jQuery UI Datepicker 自动选择今天的日期并将其设置为关联输入元素的值。除了清除beforeShow 上的输入字段之外,有没有办法防止这种情况发生,例如:

beforeShow: function(input, inst) {
    $('#calendar').val(''); 
}

...既脏又容易出错。

文档似乎没有列出任何方法。有gotoCurrent,但它只将链接移动到当前选择的日期而不是今天。我想要的是不选择今天的日期并将相关的输入字段留空。

不过,我并不是说要隐藏今天的日期 CSS 类,它已经在 here 中介绍过。

请注意,这只是内联日期选择器(绑定到 divspan 元素)的问题。工具提示日期选择器(绑定到 input 元素)不会发生这种情况。

【问题讨论】:

  • 从未见过它在输入中没有设置日期时自动设置日期。不会在文档中的演示中发生 jqueryui.com/datepicker。提供重现您的问题的演示
  • 文档页面本身有一个示例,api.jqueryui.com/datepicker/#entry-examples,标题为 A simple jQuery UI Datepicker。它仅使用$( "#datepicker" ).datepicker(); 启动,并且确实选择了今天的日期。然后在控制台中键入$('#datepicker').val() 时,您可以看到输入的值也已填充。我不确定是否事先在输入中设置了日期,但我会说不是。
  • 这只突出显示当前日期...它不会将其设置为值
  • @charlietfl 看起来你是对的,对于 datepicker 附加到 input 元素的情况。但是,不幸的是,内联日期选择器仍然存在问题,根据 jQuery 文档,当它附加到 divspan 时会呈现该问题。我更新了问题以反映这一点。

标签: javascript jquery jquery-ui jquery-ui-datepicker


【解决方案1】:

如果您只想防止今天日期的突出显示覆盖 highlight 类,并使用与默认类相同的属性

$("#datepicker").datepicker();
.ui-state-highlight,
.ui-widget-content .ui-state-highlight,
.ui-widget-header .ui-state-highlight {
  border: 1px solid #c5c5c5;
  background: #f6f6f6;
  font-weight: normal;
  color: #454545;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<style>
.ui-state-highlight,
.ui-widget-content .ui-state-highlight,
.ui-widget-header .ui-state-highlight {
  border: 1px solid #c5c5c5;
  background: #f6f6f6;
  font-weight: normal;
  color: #454545;
}
</style>

<p>Date:
  <input type="text" id="datepicker">
</p>

【讨论】:

  • 但是如果我选择了这将不会突出显示当前日期。
【解决方案2】:

你可以这样做:

$('.datepicker').datepicker({
  beforeShowDay: function(date) {
    var _date = jQuery.datepicker.formatDate('yy-mm-dd', date);
    return [new Date().toJSON().slice(0,10) != _date]
  }
});

Online Demo (fiddlejs)

【讨论】:

  • 谢谢,但是在beforeShowDay 上为今天的日期返回 false 会使今天的日期无效,这不是我想要的。我需要它像其他日期一样保持可选,我只需要阻止 Datepicker 自动选择它。
猜你喜欢
  • 2014-08-10
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2023-03-11
  • 2015-12-03
  • 1970-01-01
相关资源
最近更新 更多