我在使用Knockout填充的CRUD表上工作。 jQuery Click事件不能处理新的推送元素到可观察数组
点击活动功能
$('td').on('click', function () {
var spanElement = $(this).find('span');
$(this).find('span').hide();
$(this).find('input').show().select().on('blur', function () {
$(this).hide();
spanElement.show();
});
});
此代码适用于所有已加载的行,但不适用于我使用添加按钮添加的行。
为什么会这样?如何解决?
答案 0 :(得分:2)
它不起作用,因为您将事件处理程序直接添加到表格单元格中。添加新行时,永远不会添加click元素。
要解决此问题,请将click事件应用于表并让事件委派接管
$('table').on('click', 'td', function () {
var spanElement = $(this).find('span').hide();
$(this).find('input').show().select().on('blur', function () {
$(this).hide();
spanElement.show();
});
});
答案 1 :(得分:1)
试试这个
$(document).on('click', 'table td', function () {
//Your Functions
});