0

so we can write a function on keyup by using Jquery on:

$('#thing').on('keyup',function(){
    //code goes here
});

but, if you want to use ajax or create the #thing dynamically in javascript, then this will not work.

In an instance where one dynamically creates an element like that, you use the document. The following would be used:

$(document).on('keyup','#thing',function(){
    //code goes here
});

But what if we want multiple events???

The non-dynamic version is allowed. Let's say we want keyup and keypress:

$('#thing').on('keyup keypress',function(){
    //code goes here
});

but the dynamic version below will not work:

$(document).on('keyup keypress','#thing',function(){
    //code goes here
});

how can one use $(document).on for multiple events on a specific selector? if it is not possible, what would be the equivalent enabling dynamic creation?

4

1 回答 1

0

似乎在 chrome 中对我来说工作正常。您确定您创建的元素会触发您绑定的事件吗?也可能是浏览器兼容性问题。

$('body').append('<input type="text" id="thing" placeholder="thing">');


$(document).on('keyup keypress','#thing',function(){
   alert('event tiggered');
});

http://jsfiddle.net/aY29a/

于 2013-08-29T00:11:52.483 回答