1 回答

TA貢獻(xiàn)1876條經(jīng)驗 獲得超5個贊
專注于第一個空輸入非常容易,但是當(dāng)輸入為空時自動向后移動感覺違反直覺......如果您愿意,您可以添加一個鍵盤處理程序來查看用戶是否在空輸入中輸入退格鍵;如果是,請將焦點移回。
也就是說,使用三個輸入框而不是一個的原因是什么?:)
const inputs = document.querySelectorAll(".input-grouped");
function alwaysFocusFirstEmptyInput(event) {
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].value === "") {
inputs[i].focus();
break;
}
}
}
[...inputs].forEach((input) => {
input.addEventListener("input", alwaysFocusFirstEmptyInput, null);
});
<input class="input-grouped" id="first" maxlength="1" value="">
<input class="input-grouped" id="second" maxlength="1" value="">
<input class="input-grouped" id="third" maxlength="1" value="">
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報