我需要使用Enter键进入具有很多行的表中的下一个输入字段。我尝试这样:
$(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()
}
});
由于OP已更新问题。利用
$('.inputs').keydown(function (e) {
if (e.which === 13) {
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
}
});
演示,如前所述,我已经使用过class="inputs"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句