How To Handling Multiple Events On jQuery
i want to handling multiple events in one event function. i used jquery ".on" function but it doesn't worked for multiple events. on the following code doesn't work:
$("input.action:checkbox").on('click,change',function(){
alert('bla bla');
});
When clicked or changed any item on this selector i want fire to the same function. but i dont want to in another function handling. as can be seen following example code i dont want:
$("input.action:checkbox").on('click',function(){
changePlease(); // in this func will process
});
$("input.action:checkbox").on('change',function(){
changePlease(); // in this func will process
});
have u got any idea?
What you are doing is correct ,but a small syntax issue,Just remove comma between the event
names ..
$("input.action:checkbox").on('keyup keypress blur change',function(){
alert('bla bla');
});
And more possibilities
To answer your question, best way is:
$("input[type=checkbox].action").on('click change',changePlease);
Just use the name of function as callback, without '()'. BTW, using input[type=checkbox]
improve selector performance.
下一篇: 如何在jQuery上处理多个事件