使用方法
const inputs = document.querySelectorAll(".otp-field > input");
const button = document.querySelector(".btn");
window.addEventListener("load", () => inputs[0].focus());
button.setAttribute("disabled", "disabled");
inputs[0].addEventListener("paste", function (event) {
event.preventDefault();
const pastedValue = (event.clipboardData || window.clipboardData).getData(
"text"
);
const otpLength = inputs.length;
for (let i = 0; i < otpLength; i++) {
if (i < pastedValue.length) {
inputs[i].value = pastedValue[i];
inputs[i].removeAttribute("disabled");
inputs[i].focus;
} else {
inputs[i].value = ""; // Clear any remaining inputs
inputs[i].focus;
}
}
});
inputs.forEach((input, index1) => {
input.addEventListener("keyup", (e) => {
const currentInput = input;
const nextInput = input.nextElementSibling;
const prevInput = input.previousElementSibling;
if (currentInput.value.length > 1) {
currentInput.value = "";
return;
}
if (
nextInput &&
nextInput.hasAttribute("disabled") &&
currentInput.value !== ""
) {
nextInput.removeAttribute("disabled");
nextInput.focus();
}
if (e.key === "Backspace") {
inputs.forEach((input, index2) => {
if (index1 <= index2 && prevInput) {
input.setAttribute("disabled", true);
input.value = "";
prevInput.focus();
}
});
}
button.classList.remove("active");
button.setAttribute("disabled", "disabled");
const inputsNo = inputs.length;
if (!inputs[inputsNo - 1].disabled && inputs[inputsNo - 1].value !== "") {
button.classList.add("active");
button.removeAttribute("disabled");
return;
}
});
});
站长提示:
1. 苦力吧素材官方QQ群:
950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励
2K币。
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服