focusing on next input (jquery)

前端 未结 12 2451
慢半拍i
慢半拍i 2020-11-30 05:09

I\'ve got four inputs that each take one number. What I want to do is set the focus automatically to the next input once the number has been set. They all have the class \"i

12条回答
  •  不知归路
    2020-11-30 05:31

    This is the code that complete your all needs.

    $(".input").keyup(function(e) {
    
    if (e.which == 8 || e.which == 46){ 
        //backspace / Delete
        $(this).val('');
        $(this).prevAll('input:first').focus();
    }
    else
    { 
        var spcl_arr = ["~","!","@", "#", "$", "%", "^", "&", "*", "(", ")", "+","-", "=", "." ,"/"];
       if(e.which == 13){ // Enter Kay
          return false;
       }
      else if(jQuery.inArray($(this).val(), spcl_arr) !== -1 ){ 
             $(this).val('');
             $(this).focus();
            return false;
       }else{ 
           var regex = new RegExp("^[a-zA-Z0-9]+$");
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) { 
                 if (this.value.length == this.maxLength) {
                     $(this).next('.input').focus();
                 }
            }else{
                $(this).val('');
                $(this).focus();
                return false;
            }
        }
    }
    

    });

提交回复
热议问题