没有返回$(’input:checkbox’)。change(function(){?

我正在测试我在这里提供的第二个解决方案,用于检查和记录已经检查过的每个复选框。 第一个解决方案就像一个魅力,但第二个更有效的解决方案甚至没有运行。

for (x in aL.results) { aLIds.push(aL.results[x].listing_id); aLTitles.push(aL.results[x].title); aLQuantities.push(aL.results[x].quantity); aLDescs.push(aL.results[x].description); aLTaxPaths.push(aL.results[x].Tax_path); aLTaxIds.push(aL.results[x].Tax_id); aLTags.push(aL.results[x].tags); aLUrls.push(aL.results[x].url); aLPrices.push(aL.results[x].price); aLViews.push(aL.results[x].views); aLHearts.push(aL.results[x].num_favorers); $('#tblListings').append( '' + '' + '' + aLQuantities[x] + '' + '' + aLTitles[x] + '' + '' + aLPrices[x] + '' + '' + aLViews[x] + '' + '' + aLHearts[x] + '' + '' ); } $('input:checkbox').change(function() { alert('ah'); var uLIndex = $('input:checkbox:checked').map(function() { return this.value; }).get(); console.log(uLIndex); }); 

对于动态创建元素,我们不能直接绑定事件做这样的事情或实时函数,这个函数在1.9版本中删除,所以确保你使用的是什么版本的jquery或者你可以在jquery官方网站上找到很多方法

 $('body').on("change","input:checkbox",function() { alert('ah'); var uLIndex = $('input:checkbox:checked').map(function() { return this.value; }).get(); console.log(uLIndex); }); 

更新

添加一个类到复选框检查

 $('#tblListings').append( '' + '' + '' + aLQuantities[x] + '' + '' + aLTitles[x] + '' + '' + aLPrices[x] + '' + '' + aLViews[x] + '' + '' + aLHearts[x] + '' + '' ); 

然后在所有复选框上使用类名检查绑定事件,如下所示。

 $('body').on("change",".check",function() { alert('ah'); var uLIndex = $('input:checkbox:checked').map(function() { return this.value; }).get(); console.log(uLIndex); });