2016-01-16 28 views
0

我有一个表,它的行通过一个Ajax查询响应等产生:jQuery函数不要再追动态生成的ID

var inp = '<input id="new_po_qty" style="text-align:center" type="text" value="1">'; 
$('<tr id="new_po_item_row">').html('<td style="text-align:center">' + sku_id + '</td><td style="text-align:center">' + inp + '</td><td id="new_po_cp" style="text-align:center">' + cost_price + '</td><td id="new_po_total_cp" style="text-align:center">' + cost_price + '</td>').appendTo('#new_po_body'); 

现在,我写了一个jQuery函数赶在表行的inp变量的变化。但jquery函数没有捕捉到我在输入框中做的更改。

jQuery函数:

$('#new_po_qty').on('change paste', function(){ 
    alert('changes made.'); 
    $.niftyNoty({ 
     type:"primary",icon:"",title:"Start Adding Products.",container:"floating",timer:5000 
    }); 
}); 

我我做错了什么与功能是什么?

+0

请参阅http://stackoverflow.com/questions/8110934/direct-vs-delegated-jquery-on –

回答

2

使用事件代表团:

$(document).on('change paste', '#new_po_qty', function(){ 
    alert('changes made.'); 
    $.niftyNoty({ 
     type:"primary",icon:"",title:"Start Adding Products.",container:"floating",timer:5000 
    }); 
}); 

jQuery .on() docs