jQuery 如何在datepicker jquery中选择日期未来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9120309/
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
How to select date future in datepicker jquery
提问by HymanTurky
i'm using datePicker
in this way:
我正在datePicker
以这种方式使用:
$("#dataStart").datepicker({
beforeShow: function () { $('#ui-datepicker-div').css('z-index',9999); },
dateFormat: 'mm/dd/yy'});
$("#dataEnd").datepicker({
beforeShow: function () { $('#ui-datepicker-div').css('z-index',9999); },
dateFormat: 'mm/dd/yy'});
I want to force select a date in future and not in the past, is there a possibility to do it? if so, is possible to change in a dynamic way for the second datepicker, so if user select a date, in the second field he has to select a date next to the first selected. i hope i ask as well my question :) Thanks all!
我想强制选择未来而不是过去的日期,是否有可能做到?如果是这样,可以动态更改第二个日期选择器,因此如果用户选择一个日期,则在第二个字段中,他必须选择第一个所选日期旁边的日期。我希望我也问我的问题:) 谢谢大家!
回答by Jeff B
minDate:1
should only allow dates tomorrow and onward. Then add an onselect
to restrict dataEnd
:
minDate:1
应该只允许明天和以后的日期。然后添加一个onselect
限制dataEnd
:
$("#dataStart").datepicker({
minDate: 1,
onSelect: function(theDate) {
$("#dataEnd").datepicker('option', 'minDate', new Date(theDate));
},
beforeShow: function() {
$('#ui-datepicker-div').css('z-index', 9999);
},
dateFormat: 'mm/dd/yy'
});
Reference: http://blog.alagad.com/2009/04/20/playing-with-jquery-datepicker/
参考:http: //blog.alagad.com/2009/04/20/playing-with-jquery-datepicker/
Example: http://jsfiddle.net/jtbowden/F39kt/
回答by ShankarSangoli
If you return [false] in beforeShowDay
event of datepicker it disables that day. You can compare the date and the current date and return [true/false] accordingly.
如果您在beforeShowDay
datepicker 事件中返回 [false],它将禁用当天。您可以比较日期和当前日期并相应地返回 [true/false]。
Try this.
尝试这个。
$("#dataStart").datepicker({
beforeShow: function () {
$('#ui-datepicker-div').css('z-index',9999);
},
dateFormat: 'mm/dd/yy',
});
$("#dataEnd").datepicker({
beforeShow: function () {
$('#ui-datepicker-div').css('z-index',9999);
},
dateFormat: 'mm/dd/yy',
beforeShowDay: function(date){
return [(date > ($("#dataStart").datepicker("getDate")
|| new Date()))];
}
});
回答by DrewT
This is an old thread but it's somewhat unclarified. Could be because of changes to the jQuery UI Widget itself, but for anyone stumbling upon this:
这是一个旧线程,但它有点不清楚。可能是因为 jQuery UI Widget 本身发生了变化,但对于任何遇到此问题的人来说:
To limit datepickers scope to future dates only - set the minDate
param to 0.
要将日期选择器范围限制为未来日期 - 将minDate
参数设置为 0。
To limit to past dates only you would set the maxDate
param to 0.
要限制为过去的日期,您可以将maxDate
参数设置为 0。
This is nice because it avoids having to call a new Date()
function as is suggested elsewhere in this thread.
这很好,因为它避免了必须调用new Date()
该线程中其他地方建议的函数。
Example:
例子:
$('.your_datepicker').datepicker({
minDate: 0
});
回答by Sid
var myDate = new Date();
myDate.setDate(myDate.getDate()+10);
var prettyDate =(myDate.getMonth()+1) + '/' + myDate.getDate() + '/' + myDate.getFullYear();
$("#thedate").datepicker({dateFormat: 'mm/dd/yy',altFormat: 'yyyymmdd'});
$("#thedate").val(prettyDate);
This is how I do it. 'thedate' is my datepicker. I'm bumping the date to today + 10 days.
我就是这样做的。'thedate' 是我的日期选择器。我将日期推迟到今天 + 10 天。
回答by bmoran
Try this...
尝试这个...
var dates=$("#from, #to").datepicker({
changeMonth:true, 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 );
}
});
回答by Jasper
http://jqueryui.com/demos/datepicker/.
http://jqueryui.com/demos/datepicker/。
Look at the minDate
option. You can set it dynamically with the option
method.
看minDate
选项。您可以使用该option
方法动态设置它。