admin管理员组文章数量:1327885
I can not figure out why the following code does not work. JSFIDDLE LINK
$(document).ready(function () {
addInput();
});
var limit = 30;
function addInput() {
var numberOfRows = $("#shipping tr").size();
var id = numberOfRows;
if (numberOfRows == limit) {
alert("You have reached the limit of adding " + limit + " inputs");
} else {
$('#shipping').append('<tr id="rate' + id + '"></tr>');
$('tr#rate' + id).append('<td></td>');
$('tr#rate' + id).append('<td><input type="text" name="rate" /></td>');
}
$('input[name=rate]').on('keyup', 'input', function () {
alert('YAY');
return false;
});
}
I am trying to assign a keyup function to the inputs that I add dynamically.
Expected output: YAY! inside a popup box
Please help!
I can not figure out why the following code does not work. JSFIDDLE LINK
$(document).ready(function () {
addInput();
});
var limit = 30;
function addInput() {
var numberOfRows = $("#shipping tr").size();
var id = numberOfRows;
if (numberOfRows == limit) {
alert("You have reached the limit of adding " + limit + " inputs");
} else {
$('#shipping').append('<tr id="rate' + id + '"></tr>');
$('tr#rate' + id).append('<td></td>');
$('tr#rate' + id).append('<td><input type="text" name="rate" /></td>');
}
$('input[name=rate]').on('keyup', 'input', function () {
alert('YAY');
return false;
});
}
I am trying to assign a keyup function to the inputs that I add dynamically.
Expected output: YAY! inside a popup box
Please help!
Share Improve this question asked Apr 19, 2013 at 3:35 masterialmasterial 2,2169 gold badges35 silver badges49 bronze badges 1- see the demo in answer.. – Dipesh Parmar Commented Apr 19, 2013 at 3:40
2 Answers
Reset to default 7Attach an keyup event handler to shipping table, and the event will bubble up from the input[name="rate"]
to shipping table:
$('#shipping').on('keyup', 'input[name="rate"]', function () {
alert('YAY');
return false;
});
DEMO
You need to add delegation to document
because its added to the document
.
Example
$(document).on('keyup', 'input[name="rate"]', function () {
alert('YAY ');
});
Working Demo
本文标签:
版权声明:本文标题:javascript - How do I implement a keyup function with a dynamically create input element with JQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223193a2435584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论