最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - On enter key move to next cell down in a table - Stack Overflow

programmeradmin4浏览0评论

I am trying to create a form where, if you press enter key on the serial column, the cell below bees selected. Can someone please solve the correct jquery formula for this task?

I am trying

$('.serial').keypress(function(e) {
    console.log(this);
    if (e.which == 13) {
        $(this).nextAll('#serial').first().focus();
        e.preventDefault();
    }
});

/

I am trying to create a form where, if you press enter key on the serial column, the cell below bees selected. Can someone please solve the correct jquery formula for this task?

I am trying

$('.serial').keypress(function(e) {
    console.log(this);
    if (e.which == 13) {
        $(this).nextAll('#serial').first().focus();
        e.preventDefault();
    }
});

http://jsfiddle/tYFcH/4/

Share Improve this question edited Jan 23, 2013 at 1:29 Jeff 12.8k5 gold badges35 silver badges61 bronze badges asked Jan 23, 2013 at 1:20 user2002232user2002232 231 silver badge5 bronze badges 1
  • 1 '#serial' !== '.serial' – epascarello Commented Jan 23, 2013 at 1:23
Add a ment  | 

3 Answers 3

Reset to default 5

Assuming you want to move to the cell below when hitting enter in any input, use this:

$('table input').keypress(function(e) {
    if (e.keyCode == 13) {
        var $this = $(this),
            index = $this.closest('td').index();

        $this.closest('tr').next().find('td').eq(index).find('input').focus();
        e.preventDefault();
    }
});

Here's your fiddle: http://jsfiddle/tYFcH/13/

$('.serial').keypress(function(e) {
    console.log(this);
    if (e.which == 13) {
        $(this).closest('tr').next().find('input.serial').focus();
        e.preventDefault();
    }
});

http://jsfiddle/tYFcH/6/

The next-in-dom plugin was written for this purpose. Disclaimer: I wrote it

http://techfoobar./jquery-next-in-dom/

$('.serial').keypress(function(e) {
    if (e.which == 13) {
        $(this).nextInDOM('.serial').focus();
    }
});
发布评论

评论列表(0)

  1. 暂无评论