2016-01-16 63 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