
我的需求是在每次输入完一个input框以后(只能输入一个数字),下一个input锁定,类似微信支付密码那样的交互;
html:
<form id="voCash" name="card" method="post">
<input id="pac1" class="n1" type="tel" value="" maxlength="1" onKeyUp="return T1_onkeyup()" name="T1" tabindex = "1">
<input id="pac2" class="n2" type="tel" value="" maxlength="1" onKeyUp="return T2_onkeyup()" name="T2" tabindex = "2">
<input id="pac3" class="n3" type="tel" value="" maxlength="1" onKeyUp="return T3_onkeyup()" name="T3" tabindex = "3">
<input id="pac4" class="n4" type="tel" value="" maxlength="1" onKeyUp="return T4_onkeyup()" name="T4" tabindex = "4">
</form>
js:
function T1_onkeyup() {
if(document.card.T1.value.length==1){
document.card.T2.focus();
}
}
function T2_onkeyup() {
if(document.card.T2.value.length==1){
document.card.T3.focus();
}
}
function T3_onkeyup() {
if(document.card.T3.value.length==1){
document.card.T4.focus();
}
}
function T4_onkeyup() {
if(document.card.T4.value.length==1){
document.card.T1.focus();
}
}
我主要是在移动端实现,在安卓上面没问题,在ios上这个功能不能实现(每次的只能单击下一个输入框,重新跳出键盘),故问这代码有没有问题,或者有更好的方案?
之前想用p改写input标签,然后input标签上的很多属性在p标签上不够支持,maxlength等等?还没找到原因
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你只需要一个
input即可。css
js
https://jsfiddle.net/scroller/gLn3nzaf/
最简单的办法就是<input type="text" max-length="4" />,然后用css模拟你要的效果,例如设置background-image以及文字大小,字符间隔。不但实现起来容易,而且处理的时候也不需要对4个input的值进行组合了。
补充:背景图应该类似 [] [] [] [[]] [] [] []
[[]]表示focus效果,然后onkeyup判断一下当前value.length,然后平移背景图就好了。