1. 程式人生 > >js input框輸入1位數字後自動跳到下一個input框聚焦

js input框輸入1位數字後自動跳到下一個input框聚焦

key pre select all txt don sel this document

// input框輸入1位數字後自動跳到下一個input聚焦
function goNextInput(el){
    var txts = document.querySelectorAll(el);
    for(var i = 0; i<txts.length;i++){
      var t = txts[i];
      t.index = i;
      t.setAttribute("readonly", true);
      t.onkeyup=function(){
          this.value=this.value.replace(/^(.).*$/,‘$1‘);
          
var next = this.index + 1; if(next > txts.length - 1) return; txts[next].removeAttribute("readonly"); if (this.value) { txts[next].focus(); } } } txts[0].removeAttribute("readonly"); }

調用如下:goNextInput(‘.code-num‘);

js input框輸入1位數字後自動跳到下一個input框聚焦