我有几个输入,它们是动态创建的
<input style="width:100%" class="fileDescription" type="text" name="' + descriptionId + '" id="' + descriptionId + '" placeholder="Write a file description, hit Enter to save" />
我想将 KeyPress 事件附加到所有这些输入并传递输入的 ID。怎么做?我尝试使用下面的代码来附加事件,但这不起作用。我也不知道如何将输入字段的 ID 传递给绑定(bind)事件。
$('.fileDescription').bind('keypress', function(e) {
e.preventDefault();
alert("Works!");
});
最佳答案
试试这个:
$('.fileDescription').live('keypress', function(e) {
e.preventDefault();
alert(this.id);
});
您将在 jsfiddle 示例中看到动态添加的第二个元素是如何工作的。
关于jquery - 将多个输入绑定(bind)到 OnKeyPress 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5431840/