0

我想在一个事件函数中处理多个事件。我使用了 jquery ".on" 函数,但它不适用于多个事件。以下代码不起作用:

$("input.action:checkbox").on('click,change',function(){
    alert('bla bla');
});

当单击或更改此选择器上的任何项目时,我希望触发相同的功能。但我不想在另一个函数处理中。正如我不想要的示例代码所示:

    $("input.action:checkbox").on('click',function(){
        changePlease(); // in this func will process 
    });

  $("input.action:checkbox").on('change',function(){
      changePlease(); // in this func will process 
    });

你有什么想法吗?

4

2 回答 2

4

你所做的是正确的,但是一个小的语法问题,只需删除event名称之间的逗号..

$("input.action:checkbox").on('keyup keypress blur change',function(){
    alert('bla bla');
});

还有更多的可能性

于 2013-06-28T09:49:23.197 回答
1

要回答您的问题,最好的方法是:

$("input[type=checkbox].action").on('click change',changePlease); 

只需使用函数名称作为回调,不带'()'。顺便说一句,使用input[type=checkbox]提高选择器性能。

于 2013-06-28T09:52:10.027 回答