如何在填充4个字符后移动下一个输入标签 [英] how to move next input tab after fill 4 characters

查看:124
本文介绍了如何在填充4个字符后移动下一个输入标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有另一个问题,如果我需要更改第二或第三个字段,我已经填写了所有字段。



我试过下面的代码,

 (function(){
$(#num1,#num2,#num3,#num4)on(keyup,function(t){
//添加要接受的键,例如退格键或箭头键等。
if(t.keyCode!= 13& t.keyCode!= 8& t.keyCode!= 37&& ; t.keyCode!= 38& t.keyCode!= 39& t.button!= 1& t.button!= 2& t.button!= 3){
var maxlength = 4;
var num1 = $(#num1)。val()length;
var num2 = $(#num2)。
var num3 = $(#num3)。val()。length;
var num4 = $(#num4)。 .val($(this).val()。replace(/ [^ 0-9\。] / g,''));

if(num1> = 4){
$(#num2)。focus();
}
if(num2> = 4){
$(#num3)。focus b $ b}
if(num3> = 4){
$(#num4)。focus();
}

if(num4& = 4){
$(#num4)。val($(#num4)。val()。substr(0,4));
}
}

});
})();


解决方案

您粘贴的代码有错误的if语句。让我们试着理解这个问题。以下是您的代码的工作原理



在第一个字段中输入值。到达四个字符转到下一个。在第二个字段中输入。到达四个字符转到下一个。在第三个字段中输入。到达四个字符转到下一个。在第四个字段中输入。到达四个字符停止。



现在的问题是当你开始编辑。当你编辑第二行。你的if命令在同一时间告诉检查第三和第四行。所以如果第三行有四个字符转到第四。如果第四行有四个字符停止。因此,您无法编辑第二行,除非您删除第三行和第四行中写的任何内容。



Bhavesh分享的以下链接确实是您想要的。根据需要将最大限制增加到4。但我希望您能够了解问题所在,以便您日后不要犯同样的错误。

  $ .inputs)。keyup(function(){
if(this.value.length == this.maxLength){
$(this).next ;
}
}); =nofollow> JSFIDDLE LINK


I have another question, i have filled all fields if i need to change number second or third field. changes can not be done and it goes last input field.

I have tried below code,

    (function (){
$("#num1, #num2, #num3, #num4").on("keyup", function (t) {
    //add the key's you want to accept such as backspace or arrow keys etc.
    if (t.keyCode != 13 && t.keyCode != 8 && t.keyCode != 37 && t.keyCode != 38 && t.keyCode != 39 && t.button != 1 && t.button != 2 && t.button != 3) {
    var maxlength = 4;
    var num1 = $("#num1").val().length;
    var num2 = $("#num2").val().length;
    var num3 = $("#num3").val().length;
    var num4 = $("#num4").val().length;
    $(this).val($(this).val().replace(/[^0-9\.]/g,''));

    if ( num1 >= 4) {
    $("#num2").focus();
    }
    if( num2 >= 4){
      $("#num3").focus();
    }
    if( num3 >= 4){
      $("#num4").focus();
    }

    if (num4>=4){
       $("#num4").val($("#num4").val().substr(0,4));
    }
    }

  });   
})();

解决方案

The code you have pasted has wrong if statement. Lets try to understand the problem. Following is how your code works

Input value in first field. Reach four characters go to next. Input in second field. Reach four characters go to next. Input in third field. Reach four characters go to next. Input in fourth field. Reach four characters stop.

Now the problem is when you start editing. When you edit second line. Your if command at the same time is telling to check third and fourth line. So if third line has four characters go to fourth. And if fourth line has four characters stop. Hence you aren't able to edit second line unless you delete whatever is written in third line and fourth line.

The below link shared by Bhavesh does exactly what you want. Increase max limit to 4 as desired. But I hope you are able to understand where the problem is so you don't make similar mistake in the future.

$(".inputs").keyup(function () {
  if (this.value.length == this.maxLength) {
    $(this).next('.inputs').focus();
  }
});

JSFIDDLE LINK

这篇关于如何在填充4个字符后移动下一个输入标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆