焦点使用Jquery

时间:2016-04-20 07:25:15

标签: javascript jquery html

$(this).closest('tr').next() td:eq(7).focus();

这有什么错误?我是jQuery的新手。如何使用trtdtable索引进行关注?

$('.gridfield').keypress(function(e) {
    console.log(this);
    if (e.which == 13) {    
        var row_index = $(this).parent().index();    
        $(this).closest('tr').next() td:eq(row_index).focus();     
        e.preventDefault();
    }
});

1 个答案:

答案 0 :(得分:1)

语法不是很正确。你可以使用这个:

$(this).closest('tr').next().find('td').eq(row_index).focus();     

或者这个:

$(this).closest('tr').next().find('td:eq(' + row_index + ')').focus();     

它们在逻辑上是相同的。

  

每个单元格包含输入字段

在这种情况下,使用以下任何一种:

$(this).closest('tr').next().find('td').eq(row_index).find('input').focus(); 
$(this).closest('tr').next().find('td:eq(' + row_index + ') input').focus();