我正在使用来自KelvinLuck的jQuery日期选择器插件。它非常简单明了,但我想绑定它以进行任何更改并在控制台中显示日期。该函数存在,但它仅在初始化日期选取器的函数中工作。我想在该函数之外使用绑定函数。这是一个jsFiddle。
.HTML
<div class="date-pick"></div>
.JS
$(function() {
$('.date-pick')
.datePicker({
createButton: false,
displayClose: false,
closeOnSelect: true,
selectMultiple: true,
inline: true,
startDate: '01/01/2005',
endDate: '31/12/2011'
})
.dpSetSelected(
'18/06/2011'
)
.bind(
'click',
function() {
$(this).dpDisplay();
this.blur();
return false;
}
)
.bind(
'dateSelected',
function(e, selectedDate, $td, state) {
console.log('You ' + (state ? '' : 'un') // wrap
+ 'selected ' + selectedDate);
}
)
});
当前代码成功地将日期显示在控制台中,但我希望该绑定函数位于当前函数之外。所以像这样:
$('date-pick').bind(
'dateSelected',
function(e, selectedDate, $td, state) {
console.log('You ' + (state ? '' : 'un') // wrap
+ 'selected ' + selectedDate);
}
)
这可能吗?
这是您的解决方案:
$(function() {
$('.date-pick')
.datePicker({
createButton: false,
displayClose: false,
closeOnSelect: true,
selectMultiple: true,
inline: true,
startDate: '01/01/2005',
endDate: '31/12/2011'
});
// Your binding event functions have been removed from here. Its declared outside this function.
});
$('.date-pick').bind( // You did not put `.` here
'dateSelected',
function(e, selectedDate, $td, state) {
console.log('You ' + (state ? '' : 'un') // wrap
+ 'selected ' + selectedDate);
}
);