我有一个ajax调用,它使用以下代码为响应中的每条记录向数据表添加一些行:
strAppName = data.Application_Name
maintCost = '<input class="maintCostField" appid="' + data.Application_ID + '" type="text" placeholder="$">';
$('#myReport').dataTable().fnAddData([
strAppName,
maintCost,
etc,
etc
]);
我已尝试使用以下选择器/事件来捕获对文本框的更改,但它们都没有触发。它们在document.ready部分......
$(".maintCostField").bind('change',function () {
val = $(this).val();
app = $(this).attr('appid');
alert('Updating app# ' + app + ' with ' + val);
});
$(".maintCostField").on('change',function () {
val = $(this).val();
app = $(this).attr('appid');
alert('Updating app# ' + app + ' with ' + val);
});
(我知道这个已被弃用)
$(".maintCostField").live('change',function () {
val = $(this).val();
app = $(this).attr('appid');
alert('Updating app# ' + app + ' with ' + val);
});
我做错了什么?我不明白为什么它与Click event doesn't work on dynamically generated elements或许多其他人有任何不同......
更新* 我将以下内容添加到数据表的fnDrawCallback事件中,现在它可以工作,只执行它与屏幕上的表单一样多次。
$(".maintCostField").each(function () {
this.addEventListener("change", function () {
val = $(this).val();
app = $(this).attr('appid');
alert('Updating app# ' + app + ' with ' + val);
}, true);
});