gpt4 book ai didi

jquery - 按 Enter 键聚焦表格内的下一个输入字段

转载 作者:行者123 更新时间:2023-12-03 22:55:57 24 4
gpt4 key购买 nike

我需要使用回车键移至多行表中的下一个输入字段。我试试这个:

$(function() {

$('input').keyup(function (e) {
if (e.which == 13) {
$(this).find().next('input').focus();
}
});

});

为什么不工作? :(

HTML

<table class="half">
<tr>
<td>Nome :</td>
<td><input name="ragsoc" type="text" id="ragsoc" value=""/></td>
<td>Mnemo:</td>
<td><input name="mnemo" type="text" id="mnemo" value=""/></td>
<td>Partita IVA :</td>
<td><input name="piva" type="text" id="piva" value=""/></td>
</tr>
<tr>
<td>Nome :</td>
<td><input name="ragsoc" type="text" id="ragsoc" value=""/></td>
<td>Mnemo:</td>
<td><input name="mnemo" type="text" id="mnemo" value=""/></td>
<td>Partita IVA :</td>
<td><input name="piva" type="text" id="piva" value=""/></td>
</tr>
</table>

此代码工作正常,但仅在表的第一行中:

$('input').keydown(function (e) {
if (e.which === 13) {
$(this).closest('td').nextAll().eq(1).find('input').focus()
}
});

有什么问题或遗漏吗?

最佳答案

假设 .inputs 是当前元素的同级元素。 .find() 在当前元素的后代中搜索(this)

 $('.inputs').keydown(function (e) {
if (e.which === 13) {
$(this).next('.inputs').focus();
}
});

您需要使用.closest() , .nextAll()

 $('.inputs').keydown(function (e) {
if (e.which === 13) {
$(this).closest('td').nextAll().eq(1).find('.inputs').focus();

//OR
//$(this).closest('td').next().next().find('.inputs').focus()
}
});

DEMO

由于OP已更新问题。使用

 $('.inputs').keydown(function (e) {
if (e.which === 13) {
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
}
});

DEMO ,如前所述,我使用了 class="inputs"

关于jquery - 按 Enter 键聚焦表格内的下一个输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com