为什么不能使用已定义的函数作为事件回调?
<div id="me">
<input>
</div>
$(document).ready(function(){
// $("#me").on('keyup', 'input', doit()); # doesn't work
// $("#me").on('keyup', 'input', 'doit'); # neither does this
$("#me").on('keyup', 'input', function() {
doit();
}); // well, this one works of course
});
function doit() {
console.log($("input").val());
}
最佳答案
您需要将函数作为参数传入。
$("#me").on('keyup', 'input', doit);
关于javascript - 使用定义的函数而不是匿名函数作为回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18538106/