代码之家  ›  专栏  ›  技术社区  ›  sagescrub

jquery datepicker onselect事件处理程序多次

  •  8
  • sagescrub  · 技术社区  · 16 年前

    我尝试在jQuery日期选取器对象上两次处理相同的onSelect事件。据我所知,事件可以处理多次,但当我尝试此操作时,只有一个事件处理程序被激发。它似乎触发了第二个处理程序,但不是第一个。如何在不重写第一个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'); });
    }
    
    1 回复  |  直到 16 年前
        1
  •  11
  •   tvanfosson    16 年前

    您使用的代码只是将第一个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'); } );
    });
    
    推荐文章