javascript - 有人知道为什么我的 jquery 在我的 html 表中找不到新插入的行吗?

标签 javascript jquery html

我正在制作一个动态表系统,可以在行为空时通过更改行来添加行。我的问题是新添加的行对我的事件处理程序来说是“不可见的”。 这些是我的事件处理程序:

$('tr td ').on('blur', 'input.autoupdate',function(event){
    event.stopPropagation();
    event.preventDefault();
    rid = $(this).closest('tr').attr('id');
});

$('tr td ').on('focus', 'input.autoupdate',function(e){
    e.stopPropagation();
    e.preventDefault();
    newid = $(this).closest('tr').attr('id');
    check();
});

这是我添加行的函数:

  var table_row = "<tr><td><input></td></tr>";
  $(table_row).appendTo('#auto_insert');

谁能告诉我我做错了什么?

最佳答案

这将解决您的问题:

$(document).on('blur', 'tr td input.autoupdate',function(event){
     event.stopPropagation();
     event.preventDefault();
     rid = $(this).closest('tr').attr('id');
});
$(document).on('focus', 'tr td input.autoupdate',function(e){
     e.stopPropagation();
     e.preventDefault();
     newid = $(this).closest('tr').attr('id');
     check();
});

问题是:

您的 trs 和 tds 是动态添加的,因此它们没有事件监听器来监听它们上发生的事件,这就是您在动态添加的元素上设置事件监听器的方法。

关于javascript - 有人知道为什么我的 jquery 在我的 html 表中找不到新插入的行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24801421/

相关文章:

javascript - 使用谷歌地图映射城市/邮政编码多边形

c# - JQuery $.post 在 VS 2010 实例中工作但在 IIS 7.5 中部署时不工作

javascript - Android 4,如何关闭更改事件的选择面板

javascript - 如何在 Ryan fait stick 页脚中拆分页眉和页脚之间的空间

javascript - css3 翻译成为 child 的相对定位

javascript - 如何通过javascript(而不是键盘)按下任何字符

javascript - 使用javascript从php获取json数据

jquery - 使用 CSS 或 jQuery 更改文本框光标颜色

html - 某些点击在 html 中不起作用

html - 证明跨度之间的内容