jQuery 箭头键控函数在表的最后一行添加文本框时停止

jQuery arrow keyup function stop while adding a textbox in last row in my table

本文关键字:添加 一行 文本 函数 最后 jQuery      更新时间:2023-09-26

这是我的jquery代码,我想继续我的箭头键功能。

$(document).ready(function () {
 $('input').keyup(function (e)  {
    switch (e.keyCode) {
        case 37:
           // alert('left');      
          $(this).closest('td').prev().find('input').focus();  
            break;
        case 38:
          // alert('up');
             $(this).closest('tr').prev().find('td:eq(' + $(this).closest('td').index() + ')').find('input').focus();
            break;
        case 39:
           // alert('right');
            $(this).closest('td').next().find('input').focus();  
            break;
        case 40:
           // alert('down');            
            var checkRow = $(this).closest('tr').index() + 1;
            var totalRows = $(this).closest('tbody').find('tr').length;
            if (checkRow == totalRows)
            {
                    //here's my additional table row
                     var add= $(this).closest('tr').clone().appendTo($(this).closest('tbody').parent());
                     //i want to continue arrows key for additional textbox in my new row. 
            }
             $(this).closest('tr').next().find('td:eq(' + $(this).closest('td').index() + ')').find('input').focus();   
            break;
    } }); });

不会keyup事件添加到新创建的input。而不是仅将事件添加到现有input$('input').keyup(fn)使用$(document).on('keyup', 'input', fn)。它侦听文档,并在到达每个input选择器时触发。API 文档中的示例。