JQuery UI 日期选择器:如何在呈现日历后绑定事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5201367/
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
JQuery UI datepicker: how to bind an event AFTER the calendar was rendered?
提问by bluefoot
I am using datepicker. The usual way to bind an event, lets say for example, beforeShowDay
, would be:
我正在使用datepicker。绑定事件的常用方法,例如,beforeShowDay
,将是:
$('.selector').datepicker({
beforeShowDay: function(date) { ... }
});
What I need is to bind the beforeShowDay
event after the calendar was rendered. So, let's say first I render the calendar:
我需要的是beforeShowDay
在呈现日历后绑定事件。所以,让我们先说我渲染日历:
$('.mySpecialContainer').datepicker({
//in this moment, for some reason, I am not allowed to bind nothing (that's not me who renders the calendar, that's why I am not allowed)
});
And then, if I try to bind something in the usual way:
然后,如果我尝试以通常的方式绑定某些东西:
$('.mySpecialContainer').datepicker({
beforeShowDay: function(date) { ... }
});
The calendar will be - of course - rendered again inside '.mySpecialContainer'
, instead of just binding the event. I have tried:
日历将 - 当然 - 在里面再次呈现'.mySpecialContainer'
,而不是仅仅绑定事件。我试过了:
$('.mySpecialContainer').bind('beforeShowDay', function(date) { ... });
But that was just a desperate action :)
但这只是一个绝望的行动:)
回答by Andrew Whitaker
It doesn't make sense to me why you can't call bind
on that particular datepicker
event (or, for that matter, most datepicker
events). Most other jQueryUI widgets allow late-binding of events.
我不明白为什么你不能调用bind
那个特定的datepicker
事件(或者,就此而言,大多数datepicker
事件)。大多数其他 jQueryUI 小部件允许事件的后期绑定。
However, you can try the following. hasDatepicker
is applied to datepickers, and calling .datepicker
again on those elements is notreinitializing the widget, it's just modifying an option:
但是,您可以尝试以下操作。hasDatepicker
应用于日期选择器,.datepicker
再次调用这些元素不会重新初始化小部件,它只是修改一个选项:
$('.hasDatepicker').datepicker("option", "beforeShowDay",
function(date) { ... });
回答by Sam Thompson
$('.ui-datepicker-current-day').click();
回答by Federico Martín Lara
With
和
$('.hasDatepicker').datepicker("option", "beforeShowDay",
function(date) { ... });
I have the next bug:
我有下一个错误:
TypeError: $(...).datepicker is not a function
类型错误:$(...).datepicker 不是函数
$(".hasDatepicker").datepicker('option', 'beforeShowDay', function(event) {
It seems that you don't have tested this.
看来你没有测试过这个。
回答by Chris Sobolewski
You need .live()
.
http://api.jquery.com/live/
你需要.live()
.
http://api.jquery.com/live/
$('.mySpecialContainer').live('beforeShowDay', function(date) { ... });
On second thought, I think I misunderstood the question... You need to have a datepicker without a function, and then later on give it a function? You'll probably be best off removing the datepicker and putting it back on.
再想一想,我想我误解了这个问题......你需要一个没有函数的日期选择器,然后再给它一个函数?您可能最好移除日期选择器并将其重新安装。