jquery - 动态生成文本框的按键事件

标签 jquery dynamic

tr.append($("<td/>").html('<input type="text" name="unit_price" id="unit_price"/>'));

这是我动态生成的文本框,我已经尝试了很多像下面这样的解决方案,但它不起作用帮助我极客

$("#unit_price").live("keyup", function(){  
     //Your Code to handle the click.
    }); 

$(staticAncestors).on(eventName, dynamicChild, function() {});

最佳答案

您需要使用event delegation

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

$("body").on("keyup","#unit_price", function(){  
 //Your Code to handle the click.
}); 

关于jquery - 动态生成文本框的按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943052/

相关文章:

javascript - 切换<TR>

javascript - 如何预加载大尺寸图像?

C - 动态调整二维字符数组的大小

c# - 使用 LINQ 将 List<dynamic> 转换为 List

javascript - jQuery .offset() 为 var 对象的类 `top` 获取未定义

javascript - Jquery 自动 json 命名

javascript - 在 jquery 中混合 $(this) 值

java - 我可以在java中动态引用一个对象吗?

dynamic - wget应用到 "Save As Complete Web Page"

Android:获取触发onclick事件的元素