Stop jQuery from calling next events
I write a jQuery plugin the first time and I'm wondering if there is a way to stop jQuery from running the next attached events.
Example:
$(this).submit(function(){
return $(this).do_some_validation();
}
If validation didn't pass (i.e. the function returned 开发者_StackOverflow社区false
), the form should not be submitted, but if there are any other event handlers attached, only last handler return value can prevent form from being submitted.
You may also want to look at event.stopImmediatePropagation, as stopPropagation "will not prevent other handlers on the same element from running" (for example, two submits handlers on the same form should still be triggered if using stopPropagation).
Use the stopPropagation() method:
$(this).submit(function(e){
e.stopPropagation();
return $(this).do_some_validation();
}
You may take a look at the event.stopPropagation()
function.
stopPropagation()
works on most events but may not always work on delegated events, .live()
and .delegate()
.
return false;
is a fool proof way if stopPropogation doesn't work first.
I use this functions with .live()
and never have problems
jQuery("a.entryTable, a.entryDelete").live("click", function(e) {
e.preventDefault();
e.stopPropagation();
// code
return false;
});
精彩评论