jQuery .on('change',function(){}不会触发dynamic创build的input

问题是,我有一些dynamic创build的input标签集,我也有一个函数,意味着任何时候input值改变。

$('input').on('change', function() { // Does some stuff and logs the event to the console }); 

然而, .on('change')不会触发任何dynamic创build的input,只会在页面加载时出现的项目触发。 不幸的是,这使我留下了一些束缚,因为.on意味着替代.live().delegate() ,它们都是.bind()包装:

有没有其他人有这个问题或知道一个解决scheme?

你应该给on函数提供一个select器:

 $(document).on('change', 'input', function() { // Does some stuff and logs the event to the console }); 

在这种情况下,它将按照您的预期工作。 另外,最好指定一些元素而不是文档。

阅读这篇文章,以更好地理解: http : //elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/

用这个

 $('body').on('change', '#id', function() { // Action goes here. }); 
 $("#id").change(function(){ //does some stuff; });