JQuery -- adding another event handler that calls the previous one
I'm trying to add a second event han开发者_Python百科dler to a datepicker that's already been created.
Basically I have some function that makes a datepicker with a bunch of defaults, including an onSubmit handler.
I'd like to retain the behavior of the existing onSubmit handler, but also catch the event myself for further processing.
Here's how I'm doing that:
make_datepicker( "some_id" ); //someone else's code that I can't change
var prev_submithandler = $("#some_id").datepicker('option', 'onSelect');
$("#some_id").datepicker('option', 'onSelect', function(dateText) {
prev_submithandler(dateText);
// do some other stuff
});
This seems to work great in the debugger, but when I step into the previous submit handler, the value of "this" has changed to be not the datepicker's input field, but now the entire DOMWindow object. This breaks the logic in the previous submit handler.
If I try to just add a second handler, the first one seems to be overridden (which I thought was not the expected behavior?), so I tried this approach instead.
Am I going about this all wrong? I'm kind of a javascript n00b.
not sure what you do but sounds like a scope issue, so try to use function.call or function.apply
e.g.
$("#some_id").datepicker('option', 'onSelect', function(dateText) {
prev_submithandler.call(this, dateText);
// do some other stuff
});
精彩评论