我需要使用回车键按下移动到包含许多行的表中的下一个输入字段.我试试这个:
$(function() {
$('input').keyup(function (e) {
if (e.which == 13) {
$(this).find().next('input').focus();
}
});
});
Run Code Online (Sandbox Code Playgroud)
为什么不工作?:(
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>
Run Code Online (Sandbox Code Playgroud)
此代码工作正常,但只在表的第一行:
$('input').keydown(function (e) {
if (e.which === 13) {
$(this).closest('td').nextAll().eq(1).find('input').focus()
}
});
Run Code Online (Sandbox Code Playgroud)
有什么不对或缺少?
Sat*_*pal 15
假设.inputs你是当前元素的兄弟..find()在当前元素的后代中搜索(this)
$('.inputs').keydown(function (e) {
if (e.which === 13) {
$(this).next('.inputs').focus();
}
});
Run Code Online (Sandbox Code Playgroud)
您需要使用.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()
}
});
Run Code Online (Sandbox Code Playgroud)
由于OP已更新问题.使用
$('.inputs').keydown(function (e) {
if (e.which === 13) {
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
}
});
Run Code Online (Sandbox Code Playgroud)
演示,我用过的笔记class="inputs"
| 归档时间: |
|
| 查看次数: |
33145 次 |
| 最近记录: |