jQuery 日期选择器 - 禁用过去的日期
我正在尝试使用 UI 日期选择器选择日期范围。
在“从/到”字段中,人们不应该能够查看或选择当天之前的日期。
这是我的代码:
$(function() {
var dates = $( "#from, #to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onSelect: function( selectedDate ) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
}
});
});
有人可以告诉我如何禁用当前日期之前的日期吗?
I am trying to have a date Range select using the UI date picker.
in the from/to field people should not be able to view or select dates previous to the present day.
This is my code:
$(function() {
var dates = $( "#from, #to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onSelect: function( selectedDate ) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
}
});
});
Can some one tell me how to disable dates previous the to the present date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您必须创建一个新的日期对象,并在初始化日期选择器时将其设置为
minDate
编辑 - 从您的评论中,它现在可以按预期工作 http://jsfiddle.net/nicolapeluchetti/dAyzq/1/
You must create a new date object and set it as
minDate
when you initialize the datepickersEdit - from your comment now it works as expected http://jsfiddle.net/nicolapeluchetti/dAyzq/1/
声明 dateToday 变量并使用 Date() 函数来设置它。
然后使用该变量分配给 minDate,它是日期选择器的参数。
就是这样...上面的答案真的很有帮助...继续努力吧伙计们..
Declare dateToday variable and use Date() function to set it..
then use that variable to assign to minDate which is parameter of datepicker.
That's it... Above answer was really helpful... keep it up guys..
minDate:0
对我有用。minDate:0
works for me.使用“minDate”选项来限制最早允许的日期。
值“0”表示今天(从今天起 0 天):
此处的文档: http://api .jqueryui.com/datepicker/#option-minDate
Use the "minDate" option to restrict the earliest allowed date.
The value "0" means today (0 days from today):
Docs here: http://api.jqueryui.com/datepicker/#option-minDate
补充一下:
如果您还需要防止用户手动输入过去的日期,这是一个可能的解决方案。这就是我们最终要做的(基于 @Nicola Peluchetti 的答案)。
这样做的作用是,如果用户手动键入过去的日期,则将值更改为今天的日期。
Just to add to this:
If you also need to prevent the user to manually type a date in the past, this is a possible solution. This is what we ended up doing (based on @Nicola Peluchetti's answer)
What this does is to change the value to today's date if the user manually types a date in the past.
现场演示,试试这个,
Live Demo ,try this,
这是一种简单的方法,
我们也可以禁用未来的日子
This is easy way to do this
also we can disable future days
通过设置
startDate: new Date()
By setting
startDate: new Date()
设置日期选择器的startDate属性,它可以工作,下面是工作代码
set startDate attribute of datepicker, it works, below is the working code
jQuery API 文档 - datepicker
最小可选日期。当设置为
null
时,没有最小值。支持多种类型:
日期:包含最小日期的日期对象。
数字:从今天算起的天数。例如,
2
代表从今天开始的两天
,-1
代表昨天
。字符串:格式由
dateFormat
选项定义的字符串,或相对日期。相对日期必须包含值和周期对;有效期限为
y
代表年
,m
代表月
,w
代表>周
,d
表示天
。例如,+1m +7d
表示从今天
开始的一个月零七天
。为了不显示除今天之外的先前日期
jQuery API documentation - datepicker
The minimum selectable date. When set to
null
, there is no minimum.Multiple types supported:
Date: A date object containing the minimum date.
Number: A number of days from today. For example
2
representstwo days
from today and-1
representsyesterday
.String: A string in the format defined by the
dateFormat
option, or a relative date.Relative dates must contain value and period pairs; valid periods are
y
foryears
,m
formonths
,w
forweeks
, andd
fordays
. For example,+1m +7d
representsone month and seven days
fromtoday
.In order not to display previous dates other than today
只需替换您的代码:
旧代码:
新代码:
just replace your code:
old code:
new code:
“mindate”属性应用于禁用 jquery datepicker 中传递的日期。
或者
"mindate" attribute should be used to disable passed dates in jquery datepicker.
OR
你可以简单地使用
它,对我来说效果很好。
you can simply use
it working fine for me.
我创建了禁用先前日期、禁用灵活周末(如星期六、星期日)的功能。
我们正在使用 jQuery UI datepicker 插件的 beforeShowDay 方法。
今天的日期是 9 月 15 日。我已禁用周六和周日。
I have created function to disable previous date, disable flexible weekend days (Like Saturday, Sunday)
We are using beforeShowDay method of jQuery UI datepicker plugin.
Here today's date is 15th Sept. I have disabled Saturday and Sunday.
你必须将当前日期声明为这样的变量
you have to declare current date into variables like this
new Date()
:函数获取今天的日期之前的日期已被锁定。
100% 工作
new Date()
: function get the todays dateprevious date are locked.
100% working