我不知道如何解决这个问题:
用户可以开始在输入字段中键入一些数字值。输入数字后,光标将移至下一个输入字段。但这是行不通的,如果用户输入速度太快,则意味着两个数字之间没有键输入。
因此,如果用户键入“ 12”(而不是“ 1”和“ 2”),则在第一个输入字段中应该有值“ 1”,在第二个输入字段中应该有值“ 2”,并且应该设置焦点到第三个输入字段。
<form>
<input type="password" maxlength="1" name="pin1" autofocus />
<input type="password" maxlength="1" name="pin2" />
<input type="password" maxlength="1" name="pin3" />
<input type="password" maxlength="1" name="pin4" />
</form>
$('form input').on('keydown', function(event) {
if (event.shiftKey || event.which <= 47 || event.which >= 58)
return false;
}).on('keyup', function (event) {
if (event.currentTarget.value.length >= 1)
$(event.currentTarget).next('input').focus();
});
你有没有试过用input
事件组合这两个条件,它将为多个用户操作,如工作key events
,cut/paste events
等:
$('form input').on('input keypress', function(event) {
if (event.type == "keypress" && (event.shiftKey || event.which <= 47 || event.which >= 58))
return false;
if (event.currentTarget.value.length >= 1)
$(event.currentTarget).next('input').focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="password" maxlength="1" name="pin1" autofocus />
<input type="password" maxlength="1" name="pin2" />
<input type="password" maxlength="1" name="pin3" />
<input type="password" maxlength="1" name="pin4" />
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句