简体   繁体   English

jQuery keyup,keydown在多行表中不起作用

[英]jquery keyup, keydown doesn't work in table with multiple row

I have a table which has 200 rows. 我有一个有200行的表。 Jquery keyup or keydown are not working. jQuery keyup或keydown不起作用。 My jquery code is : 我的jQuery代码是:

$('.upd_tab tbody tr td:eq(2) input').on('keyup',function(e){
        if (e.which==13)
        $(this).parent().parent().find('td').eq($(this).parent().index()+1).find('input').focus();
    });

Actually I want to focus or go to the input box which is located in next td. 实际上,我想聚焦或转到下一个td中的输入框。 This works with 1st tr but not in rest 199 trs Here is the HTML. 这适用于1st tr,但不适用于其余的199 trs这是HTML。

<tbody>
    <tr id="chz1">
        <td><input maxlength="16"/><div class="bx"></div></td>
        <td><input/><div class="bx"></div></td>
        <td><input maxlength="6"/><div class="bx"></div></td>
        <td><input /></td>
        <td><input /></td>
        <td><input value="1"/><div class="bx"><button class="sbut"></button></div><input type="hidden" class="hinp"/></td>
        <td><input /></td>
        <td><div class="bx"></div></td>
        <td><div class="bx"></div></td>
    </tr>
    <tr id="chz2">
        <td><input maxlength="16"/><div class="bx"></div></td>
        <td><input/><div class="bx"></div></td>
        <td><input maxlength="6"/><div class="bx"></div></td>
        <td><input /></td>
        <td><input /></td>
        <td><input value="1"/><div class="bx"><button class="sbut"></button></div><input type="hidden" class="hinp"/></td>
        <td><input /></td>
        <td><div class="bx"></div></td>
        <td><div class="bx"></div></td>
    </tr>
    <!-- etc -->
</tbody>

Try this one: 试试这个:

$('.upd_tab input').on('keyup',function(e){
    e = e || window.event;
    var code = e.keyCode;
    if (code == '13') {
        $(this).closest('td').next().find('input').focus();
    }
});

The problem you are having is that :eq(n) selects the nth element from the previous selection. 您遇到的问题是:eq(n)从上一个选择中选择了第n个元素。 That is, the collection of all td's that are a child of a tr of a tbody of an element with the upd_tab class. 就是说,所有td的集合都是upd_tab类的元素的tbody的tr的子代。 It behaves basically like this: $($('.upd_tab tbody tr td')[2]) (and the input below that element). 它的行为基本上是这样的: $($('.upd_tab tbody tr td')[2]) (以及该元素下方的输入)。 If you would use :eq(10) it would select the input box on the second row. 如果使用:eq(10) ,它将选择第二行的输入框。

What you want is :nth-child(3) . 您想要的是:nth-child(3)

$('.upd_tab tbody tr td:nth-child(3) input').on('keyup',function(e){
    if (e.which==13)
        $(this).parent().parent().find('td').eq($(this).parent().index()+1).find('input').focus();
});

For an easier understanding what elements are being selected, consider colouring them, for example with .css( { 'background': 'blue' } ); 为了更容易理解选择了哪些元素,请考虑为它们着色,例如使用.css( { 'background': 'blue' } ); . This will give you a visual clue what is happening. 这将为您提供视觉线索,说明正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM