jQuery .on('change',function(){}不触发动态创建的输入 [英] jQuery .on('change', function() {} not triggering for dynamically created inputs
问题描述
问题在于我有一些动态创建的输入标签集,而且我还有一个功能,可以在输入值发生变化时触发。
The problem is that I have some dynamically created sets of input tags and I also have a function that is meant to trigger any time an input value is changed.
$('input').on('change', function() {
// Does some stuff and logs the event to the console
});
然而 .on('更改')
不会触发任何动态创建的输入,仅针对加载页面时存在的项目。不幸的是,由于 .on
意味着替换 .live()
,这让我陷入了困境。和 .delegate()
所有这些都是 .bind()的包装器
:/
However the .on('change')
is not triggering for any dynamically created inputs, only for items that were present when the page was loaded. Unfortunately this leaves me in a bit of a bind as .on
is meant to be the replacement for .live()
and .delegate()
all of which are wrappers for .bind()
:/
有没有其他人遇到过这个问题或知道解决方案?
Has anyone else had this problem or know of a solution?
推荐答案
你应该提供一个选择器到上的
函数:
You should provide a selector to the on
function:
$(document).on('change', 'input', function() {
// Does some stuff and logs the event to the console
});
在这种情况下,它会按预期工作。此外,最好指定一些元素而不是文档。
In that case, it will work as you expected. Also, it is better to specify some element instead of document.
阅读本文以便更好地理解: http://elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs -on /
Read this article for better understanding: http://elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/
这篇关于jQuery .on('change',function(){}不触发动态创建的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!