javascript 输入的最大长度

uyto3xhc  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(172)

我的使命是:SMS代码单独输入,输入必须仅为一个数字。
我的输入从一行跳到下一行,当按退格键向后删除它们时,它还允许输入多个数字,这是我不想要的。
我有一个短信代码部分,如下所示:

我HTML代码如下:

<form class="form__group form__pincode">
  <input class="pincode" type="number" placeholder="·" tabindex="1" name="pincode-1" id="txt1" max="1" maxlength="1" onkeydown="move(event, '', 'txt1', 'txt2')" autocomplete="off">
  <input class="pincode" type="number" placeholder="·" tabindex="2" name="pincode-1" id="txt2" max="1" maxlength="1" onkeydown="move(event, 'txt1', 'txt2', 'txt3')" autocomplete="off">
  <input class="pincode" type="number" placeholder="·" tabindex="3" name="pincode-1" id="txt3" max="1" maxlength="1" onkeydown="move(event, 'txt2', 'txt3', 'txt4')" autocomplete="off">
  <input class="pincode" type="number" placeholder="·" tabindex="4" name="pincode-1" id="txt4" max="1" maxlength="1" onkeydown="move(event, 'txt3', 'txt4', 'txt5')" autocomplete="off">
  <input class="pincode" type="number" placeholder="·" tabindex="5" name="pincode-1" id="txt5" max="1" maxlength="1" onkeydown="move(event, 'txt4', 'txt5', 'txt6')" autocomplete="off">
  <input class="pincode" type="number" placeholder="·" tabindex="6" name="pincode-1" id="txt6" max="1" maxlength="1" onkeydown="move(event, 'txt5', 'txt6', '')" autocomplete="off">
</form>

我函数是这样的:

function move(e, p, c, n) {
    var length = document.getElementById(c).value.length;
    var maxlength = document.getElementById(c).getAttribute("maxlength");

    if (length == maxlength) {
        if (n !== "") {
            document.getElementById(n).focus();
        }
    }
    if (e.key === "Backspace") {
        if (p !== "") {
            document.getElementById(p).focus();
        }
    }
}

相关问题