主要代码:
var clone = div.clone();
clone.attr('id', sabad_kala_id);
$('.content').append(clone);
$('div#'+sabad_kala_id).replaceWith('<tr id='+sabad_kala_id+'><td width="50"><a class="del_kala" id='+sabad_kala_id+'><img src="images/delete.png" alt="delete" /></a></td><td width="50">1.</td><td width="388">'+title+'</td><td width="80" clas="mm"><input class="count" type="text" value='+count+' /></td><td width="100">'+price+' $</td><td width="120">'+price_count+' $</td></tr>');
并且这个代码在追加和替换后运行完美,用户可以编辑表格中的input.count
,下面的代码运行:
('input.count').keyup(function(e){
alert(test);
});
但是这段代码不起作用。
在动态创建元素时,您应该委派事件:
$(document).on('keyup', 'input.count', function(e){
alert('test');
});
代码不起作用,因为处理程序keyup在页面加载时执行,并且不会响应稍后添加的任何对象。使用
$(".count").live('keyup', function(){})
修复你的代码。
更正:自jQuery 1.7起使用$(document).on("keyup", ".count", null, function(){ // code here})
(参见http://api.jquery.com/live/)