ip输入框键入.或者合法数字自动选择下一个输入框效果

<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
   <div class="container">

<form class="form-horizontal" role="form">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="text" class="form-control input-sm" name="ip[]" placeholder="Email">
      <input type="text" class="form-control input-sm" name="ip[]" placeholder="Email">
      <input type="text" class="form-control input-sm" name="ip[]" placeholder="Email">
      <input type="text" class="form-control input-sm" name="ip[]" placeholder="Email">
    </div>
  </div>
</form>

    </div> <!-- /container -->

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
    <script>
    $(function(){
      var valid_number = function(num) {
        if ( (num >= 48 && num <= 57) || (num >= 96 && num <= 105) ) {
          return true;
        }
        else {
          return false;
        }
      }

      var input_number = function(num) {
        if (num >= 48 && num <= 57) {
          return num - 48;
        }
        else if(num >= 96 && num <= 105) {
          return num - 96;
        }
      }
      
      //键盘按下事件
      $("[name='ip[]']").keydown(function(e){
        console.log(e);
        var val = $(this).val();

        //如果键入不是数字并且不是.并且不是回车并且值不是空
        if (!valid_number(e.keyCode) && e.keyCode != 190 && e.keyCode != 110 && e.keyCode != 8) {
          return false;
        }

        // 如果键入退格
        if (e.keyCode == 8 && $(this).val() == '') {
          $(this).prev().focus();
          return false;
        }

        //如果键入不是退格键,但3个数字 或者(键入.且值不为空) 
        if ((val.length == 3 || (e.keyCode == 190 || e.keyCode == 110) && val != '') && e.keyCode != 8) {
          $(this).next().focus();

          //如果是数字
          if (valid_number(e.keyCode)) {
              $(this).next().val(input_number(e.keyCode));
          }
          return false;
        }

      });

        //键盘抬起事件
      $("[name='ip[]']").keyup(function(e){
        var val = $(this).val();
        //排除非数字
        val=val.replace(/[^\d]/ig,'');
        $(this).val(val);

        //判断数字范围
        if (val < 0 || val > 255){
          $(this).val('');
          return false;
        }
      });
    });
    </script>
  </body>
</html>

效果图

输入.或者够3位合格数字,自动把当前键入值赋值给下一个文本框

posted @ 2015-01-29 18:03  yo胡yo  阅读(760)  评论(0编辑  收藏  举报