我有五个文本框,其顺序与下面提到的 ID 相同
- txt名字
- txt姓氏
- txt电子邮件地址
- txt电话号码
- txt电话号码
我想要电话号码的选项卡功能(txtTelcode 和 txtTelephoneNumber)。
文本框 txtTelcode 的最大长度为 5。以下是所需的功能。
当用户填写 txtTelcode 时,输入 5 位数字后制表符应自动转移到下一个文本框 txtTelephoneNumber。
我使用了下面的代码,该代码工作正常,但我面临一个问题。
$(document).ready(function () {
$('#' + telcode).keyup(function () {
if ($.trim($('#' + telcode).val()).length == "5") {
$('#' + txtTelephoneNumber).focus();
}
});
});
问题是,如果 txtTelcode 中已经有 5 位数字,那么当用户按 Tab 键进入 txtTelcode 时,此函数会自动将它们前进到 txtTelephoneNumber,而不会在 txtTelcode 中停止。这不是我想要发生的事情;相反,即使 txtTelcode 已完成,我也需要能够对其进行切换。我该如何修改我的代码来解决这个问题,或者我还可以如何实现这个问题?
最佳答案
我修改了自己的代码,如下所述。如果通过获取 e.keyCode 按下 Tab 键,我会阻止代码运行。
对于 Shift + Tab 功能,如果通过获取 e.keyCode 按下 Shift,我会阻止代码运行。
$('#' + telcode).keyup(function (e) {
if (e.keyCode != 16) {
if (e.keyCode != 9) {
if ($.trim($('#' + telcode).val()).length == "5") {
$('#' + txtTelephoneNumber).focus();
}
}
}
});
这段代码运行良好。
关于jquery - 自动前进到 HTML 表单中的下一个字段,而不更改选项卡行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10172058/