JQuery .on('keydown',...) 不会在新元素上触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14610312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:45:27  来源:igfitidea点击:

JQuery .on('keydown',...) not triggering on new elements

jquery

提问by Deji

I looked through some related questions, but none helped me figure out my problem.

我查看了一些相关的问题,但没有一个能帮助我弄清楚我的问题。

I have my own dynamic data grid form built from scratch, where rows of data and field columns can be added/removed etc. I'm trying to improve the usability so that pressing the enter key will create a new field column and give focus to it. Here's the code that does this so far:

我有我自己从头开始构建的动态数据网格表单,可以在其中添加/删除数据行和字段列等。我正在尝试提高可用性,以便按 Enter 键将创建一个新的字段列并将焦点放在它。到目前为止,这是执行此操作的代码:

$('#data_fields').on('keydown','._field',function(e){
    if(e.which==13)
    {
        $('#addfield').click();
        var o=$(this).parent().parent();
        var l=$(o).children().length-2;
        var f=$(o).find("td:eq("+l+")").find("._field");
        $(f).focus();
    }
});

#addfieldis a button which adds the field column when clicked.

#addfield是一个按钮,单击时会添加字段列。

._fieldis an input box where the name of the field is written.

._field是一个输入框,其中写入了字段的名称。

#data_fieldsis a tr containing cells with ._fieldinput boxes in them.

#data_fields是一个包含带有._field输入框的单元格的 tr 。

However, the ._fieldinput boxes in the new columns do not have the special superelement keydown ability. Whats more, if I change the ('#data_fields')selector for .on()to (document), it doesn't even give current elements the keydown callback.

但是,._field新列中的输入框没有特殊的超元素 keydown 功能。更重要的是,如果我将('#data_fields')选择器更改为.on()to (document),它甚至不会为当前元素提供 keydown 回调。

Here's the HTML, sorry that its a bit messy but I use templates in PHP and stuff:

这是 HTML,抱歉它有点乱,但我在 PHP 和其他东西中使用模板:

<thead id="doc_fields">
  <tr id="delrow">
  <tr id="data_fields">
    <td style="padding:2px"><input class="_field" type="text" onkeyup="sync_doc_table();" onchange="sync_doc_table();" style="width:90%" value="" name="Fields[]"></td>
    <td style="padding:2px"><input class="_field" type="text" onkeyup="sync_doc_table();" onchange="sync_doc_table();" style="width:90%" value="b" name="Fields[]"></td>
    <td style="padding:2px"><input type="text" style="width:90%" value="" name="Fields[]" onchange="sync_doc_table();"></td>
    <td style="padding:2px"><input type="text" style="width:90%" value="" name="Fields[]" onchange="sync_doc_table();"></td>
    <td id="addcol" class="midi" style="width:25px" rowspan="3">
      <a id="addfield" onclick="inscol('#addcol','<td style=\'padding:2px\'><input onchange=\'sync_doc_table();\' type=\'text\' name=\'Fields[]\' value=\'\' style=\'width:90%\' /></td>');addcol('#typerow','<td><select name=\'Type[]\' style=\'width:91%\'><option value=\'integer\'>integer</option><option value=\'double\'>double</option><option value=\'string\' selected=\'selected\'>string</option></select></td>');addcol('#delrow','<td class=\'center _delly\'><a href=\'#\' onclick=\'del_doc_field($(this).parent());return false;\'><img src=\'/images/icons/x.png\' alt=\'[Del]\' /></a></td>');$('._colsp').attr('colspan', $('#addrow').attr('colspan') + 1);$('<td><input type=\'text\' name=\'Data[][]\' value=\'\' style=\'width:90%\' /></td>').insertBefore('.datarow ._delly');return false;" href="#">
    </td>
  </tr>
  <tr id="typerow">
</thead>

采纳答案by Brian Noah

From What I can tell it's because your input class that's being appended doesn't have a _fieldclass in it.

据我所知,这是因为您附加的输入类中没有_field类。