jQuery UI 日期选择器 onChangeMonthYear 和 inst 参数
$('.selector').datepicker({
onChangeMonthYear: function(year, month, inst) { ... }
});
如何使用 onChangeMonthYear 的 'inst' 参数自动选择该月的第一天?
请参阅: http://jsfiddle.net/sD8rL/
我目前正在使用下面的代码,但我觉得我应该能够以更直接的方式使用“inst”变量。
$(".datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
maxDate:0,
onChangeMonthYear: function(year, month, inst){
// set date to 1st on year or month change
// this seems bit janky, but works
$('#' + inst.id).datepicker( "setDate", month + '/1/' + year );
// Can't I use the instatnce to set the date?
// $(inst).datepicker( "setDate", month + '/1/' + year ); // fails
// inst.datepicker( "setDate", month + '/1/' + year ); // fails
// inst.selectedDay = 1; // fails
// inst.currentDay = 1; // fails
}
});
$('.selector').datepicker({
onChangeMonthYear: function(year, month, inst) { ... }
});
How can I use the 'inst' parameter of onChangeMonthYear to auto-select the first day of the month?
see: http://jsfiddle.net/sD8rL/
I am currently using the code below but I feel like I should be able to use the 'inst' variable in a more straight-forward way.
$(".datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
maxDate:0,
onChangeMonthYear: function(year, month, inst){
// set date to 1st on year or month change
// this seems bit janky, but works
$('#' + inst.id).datepicker( "setDate", month + '/1/' + year );
// Can't I use the instatnce to set the date?
// $(inst).datepicker( "setDate", month + '/1/' + year ); // fails
// inst.datepicker( "setDate", month + '/1/' + year ); // fails
// inst.selectedDay = 1; // fails
// inst.currentDay = 1; // fails
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有特殊原因您想要使用
inst
,您始终可以使用this
:查看实际操作:http://jsfiddle.net/william/sD8rL/2/。
If there is no particular reason that you want to use
inst
, you can always usethis
:See it in action: http://jsfiddle.net/william/sD8rL/2/.
William Niu 的解决方案不考虑其他日期格式(例如 dd-mm-yyyy)。
您最好从日期选择器中获取一个 Date 对象并按照您喜欢的方式修改它(即设置该月的第一天)。修改后,您可以将修改后的 Date 对象重新传递给日期选择器。
这样您就不会覆盖日期格式(可能在日期选择器初始化期间设置);)
请注意月份格式:日期选择器处理月份的 1-12 格式,而 Date 对象是 0-11。
希望这能有所帮助,再见!
Solution by William Niu doesn't consider other date formats (such as dd-mm-yyyy).
You'd better get a Date object from your datepicker and modify it the way you like (i.e., setting the first day of the month). After your modifications, you can re-pass the modified Date object to your datepicker.
This way you won't overwrite date format (possibly set during datepicker initialization) ;)
Pay attention to the month format: datepicker handles a 1-12 format for months, while the Date object is 0-11.
Hope this can help, bye!