gpt4 book ai didi

javascript跳到Maxlen上的下一个表单字段

转载 作者:行者123 更新时间:2023-11-29 18:19:55 24 4
gpt4 key购买 nike

我有一个收集电话号码的表格,有 3 个字段,当用户在特定字段中输入最大长度的数字时,我想自动跳转到下一个输入字段,但似乎无法正常工作

HTML

<!-- Other inputs above -->
<label>Telephone Number</label>
<input type="text" class="phone" size="3" maxlength="3" onkeyup="checkLen(this,this.value)">
<input type="text" class="phone" size="3" maxlength="3" onkeyup="checkLen(this,this.value)">
<input type="text" class="phone" size="4" maxlength="4" onkeyup="checkLen(this,this.value)">

asp.net 后端,所以 ID 都是动态生成的,因此很难通过 ID 获取元素。

javascript

function checkLen(x, y) {
if (y.length == x.maxLength) {
var next = x.tabIndex;
if (next < document.getElementsByClassName("phone").length) {
document.getElementsByClassName("phone").elements[next].focus();
}
}
}

最佳答案

假设您正在使用 jQuery。

您可以将它移动到下一个输入而不是尝试使用 tabindex

$(".phone").keyup(function () {
if (this.value.length == this.maxLength) {
$(this).next('.phone').focus();
}
});

同时删除 onclick 属性处理程序,因为您正在像上面那样绑定(bind)它。

关于javascript跳到Maxlen上的下一个表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19713426/

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