8

我试图在 jquery datePicker 对象上处理相同的 onSelect 事件两次。据我了解,该事件可以被多次处理,但是当我尝试这个时,只有一个事件处理程序被触发。它似乎触发了第二个处理程序,但不是第一个。如何在不覆盖第一个事件的情况下两次处理相同的 onSelect 事件?这是问题代码片段。

$(document).ready(function(){
    $('.test').datepicker();
    ...
    $('.test').datepicker('option', 'onSelect', function(dateText, inst) { alert('one'); });
}

...

$(document).ready(function(){
    $('.test').datepicker('option', 'onSelect', function(dateText, inst) { alert('two'); });
}
4

1 回答 1

11

您正在使用的代码只是将第一个 onSelect 处理程序替换为第二个。如果您想做两件事,那么您需要首先获取现有的 onSelect 处理程序,然后从您要替换它的处理程序中调用它。

$(function() {
     $('.test').datepicker();
     $('.test').datepicker('option', 'onSelect', function(dateText,inst) { alert('one'); } );
     var prevHandler = $('.test').datepicker('option','onSelect');
     $('.test').datepicker('option', 'onSelect', function(dateText,inst) { prevHandler(dateText,inst); alert('two'); } );
});
于 2010-02-11T02:15:05.090 回答