jquery 日期时间选择器设置 minDate 动态
我正在使用trentrichardson.com 的日期时间选择器。
我有一个带有两个输入字段的表单:from和to,我希望能够动态地将 minDate 设置为我的“to”字段,等于我的“来自”字段。
我知道我应该使用 beforeShow 选项,但不知道如何实现该功能。
$('.from,.to').datetimepicker({
beforeShow: customRange
});
编辑/解决方案
function customRange(input) {
if (input.id == 'to') {
var x=$('#from').datepicker("getDate");
$( ".to" ).datepicker( "option", "minDate",x );
}
else if (input.id == 'from') {
var x=$('#to').datepicker("getDate");
$( ".from" ).datepicker( "option", "maxDate",x );
} }
I guys i'm using a datetimepicker by trentrichardson.com.
I have a form with two input fields : from and to and i want to be able to set dynamically a minDate to my "to" field equal to the value of my "from" field.
I know i should use a beforShow option but don't know how to implement the function.
$('.from,.to').datetimepicker({
beforeShow: customRange
});
EDITED/SOLUTION
function customRange(input) {
if (input.id == 'to') {
var x=$('#from').datepicker("getDate");
$( ".to" ).datepicker( "option", "minDate",x );
}
else if (input.id == 'from') {
var x=$('#to').datepicker("getDate");
$( ".from" ).datepicker( "option", "maxDate",x );
} }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用 setter 和 getter 动态设置和获取 minDate:
此处解释
you can set and get minDate dynamically with setter and getter:
explained here
如果您有两个具有相应 ID
from
和to
的文本框,并且您希望选择to
的minDate
from
的日期,然后执行以下操作:If you have two textboxes with corresponding IDs
from
andto
and you want theminDate
ofto
to be the selected date offrom
then do the following:您可以限制日期选择器范围。
但是您想在“来自”上创建日期选择器时设置此范围。我为您制作了一个演示,据我所知,它似乎工作正常的问题。
You can restrict the datepicker range.
But you want to set this range on creation of the datepicker on the "from". I made you a demo which seems to work OK as far as I understand the question.
卢卡:对您的答案的一个小补充...
使用您建议的功能,第一次显示日期时间选择器时会忽略时间组件。如果您关闭日期时间选择器并再次重新打开它,时间组件会神秘地重新出现。我不太确定是什么原因造成的,但可以通过一些技巧来修复它:
luca: A small addition to your answer...
Using the function you suggested the time component is ignored the first time the datetimepicker is displayed. If you close the datetimepicker and reopen it again, the time component mysteriously reappears. I'm not quite sure what is causing this, but it can be fixed with a bit of a hack:
请使用最新版本的 bootstrap.min.js
Please use latest version of bootstrap.min.js
上述答案对我没有任何帮助。它要么不起作用,要么给出某种错误。
所以我将它们合并在一起并制作了我自己的一个。我如上所述使用了 moment.js,只需下载它并将其包含在您的页面中即可。
我确信有更好的方法可以做到这一点,但我找不到。所以我希望这对将来的人有帮助。
None of the above answers helped me at all. It either didn't work or gave some kind of error.
So I merged them all together and made my own one. I used moment.js as mentioned above, just download it and include it in your page.
I am sure there is a better way to do this but I couldn't find it. So I hope this helps someone in the future.
在 jQuery UI
datepicker
中,有许多函数有助于动态设置minDate
或maxDate
,例如onSelect
、onClose
。@Tanvir 的答案中已经提供了
onClose
的演示,请参阅此处:datepicker onClose 示例所以我会给你一个
onSelect
的例子。在此之前,我将告诉您
onSelect
和onclose
之间的区别,正如名称所示,onSelect
在用户选择日期和onClose< 后立即触发/code> 当日期选择器在选择日期后关闭时触发,仅此而已。
这里我们有两个日期选择器
pickupdate1
和pickupdate2
,我们希望将pickupdate1
的选定日期设置为pickupdate2 的 minDate
并且它应该是动态的,所以代码如下:In jQuery UI
datepicker
there is many Functions which are helpful to setminDate
ormaxDate
dynamically likeonSelect
,onClose
.demo of
onClose
is already provided in @Tanvir's answer see here: datepicker onClose exampleSo i will give you example of
onSelect
.before it i will tell you difference between
onSelect
andonclose
, as Name suggestsonSelect
fire just after user selects a date andonClose
fire when datepicker closes after date selection, that's all.here we have two datepicker
pickupdate1
andpickupdate2
, and we want to set selected date ofpickupdate1
to be minDate ofpickupdate2
and it should be dynamic, so here's the code:trentrichardson.com 上有一个示例。他们正在使用“onSelect”。对我来说,它工作得并不完美。设置 minDate 或 MaxDate 时,时间部分设置为 0,仅保留日期。我必须解决一些本地化问题。
There is an example on trentrichardson.com. They are using "onSelect". For me it's not working perfert. When setting minDate or MaxDate the time component is set to 0 and only the date remains. And I had to solve some localization problems.
这对我有用
This worked for me