gpt4 book ai didi

jquery autotab 不要制表符下一行

转载 作者:行者123 更新时间:2023-12-01 04:09:58 26 4
gpt4 key购买 nike

当文本框值长度达到最大值时,我希望它移动到下一个选项卡,或者 3 秒后它移动到下一个选项卡。这是我的代码:

$(".tab").keyup(function () {
var get = $(this);
if (get.val().length == this.maxLength) {
get.next('tr .tab').focus();
}
else {
setTimeout(function () {
get.next('tr .tab').focus();
}, 3000);
}
});

当光标位置在1时,移动2。但当光标在2时,不移动3。 enter image description here HTML 代码如下:

<table cellspacing="0" cellpadding="0">
<tr>
<td class="tds">Year / Number</td>
<td><input name="year" type="text" class="inputbox tab" maxlength="4" style="width:25px;" /> / <input name="number" type="text" class="inputbox tab" maxlength="2" style="width:25px;"/></td>
</tr>
<tr>
<td class="tds">Caption:</td>
<td><input name="caption" type="text" class="inputbox tab" style="width:500px;" /></td>
</tr>
<tr>
<td class="tds">Description:</td>
<td><input name="desc" type="text" class="inputbox tab" style="width:500px;" /></td>
</tr>
</table>

如果光标在 1 到 2 范围内,如果光标在 2 到 3 范围内,后来在 3 到 4 范围内,我如何管理焦点?

最佳答案

<强> LIVE DEMO

var timeo,                      // Used for our timeout
$TAB = $('.tab'); // Cache all your input elements for reuse

$TAB.keyup(function () {

clearTimeout(timeo); // Clear timeout as you type

var $el = $(this), // cache element
val = this.value, // get value
len = this.maxLength, // get length
i = $TAB.index( this ); // get the index of current one
// so we can increase it using +1
if (len != -1 && val.length >= len) {
$TAB.eq(i+1).focus();
}
timeo = setTimeout(function () {
$TAB.eq(i+1).focus();
}, 3000);

});

我会尝试根据您的问题解释我的更改:

  • 您的input元素并不都在同一个 <TD> 内父级,因此您不能使用 .next()得到......好吧......“不是下一个”
    将所有输入缓存在 $TAB 中多变的。现在在keyup里面我们需要跟踪index当前使用的输入的值,而不是增加 i = $TAB.index( this ); 的值你可以得到下一个索引 input从我们的元素集合中$TAB即:
    $TAB.eq(i+1) // is the next one in collection

  • 您没有清除您的setTimeout正确地,因此它会在不等我们完成输入的情况下滴答作响。我在这种情况下使用了 timeo变量来存储 timeout而且还允许我们在字段中输入时清除它。

  • 还有,if (len != -1将确保元素具有 maxlength属性和 && val.length >= len) {将检查输入的值是否大于允许的最大长度。

关于jquery autotab 不要制表符下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21865347/

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