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


// 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');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM