简体   繁体   English

动态输入框格式

[英]Dynamic input box formatting

I am trying to format an <input> box using javascript / jquery. 我正在尝试使用javascript / jquery格式化<input>框。

The Goal - As the user types, add hyphens automatically after every third character. 目标 -用户输入时,请在第三个字符后自动添加连字符。

123123123 becomes 123-123-123

I have a working code, but it is super slow and clunky. 我有一个有效的代码,但是它非常慢且笨拙。 I am looking for recommendations on how to improve the code 我正在寻找有关如何改进代码的建议

 $('#serial').keyup(function(){ $(this).val( $(this).val().trim() ); var str = $(this).val(); var newStr = str.replace(/-/g, ""); var valuesArray = newStr.split(""); var newVal = ""; while ( i = valuesArray.shift() ){ if(newVal.length === 3){ newVal += "-"; } else if (newVal.length === 7){ newVal += "-"; } newVal += i; } $(this).val(newVal); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <label for="serial">Type something magical</label> <input type="text" id="serial" name="serial"> 

Use input event istead of keyup it's very useful to track input fields changes : 使用input的事件istead keyup它是非常有用的跟踪输入字段的变化:

$('#serial').on('input', function(){

NOTE : The code seems slow because keyup won't fire until you release the key, but you can type the next character before releasing the first. 注意:代码似乎很慢,因为在释放键之前不会启动keyup ,但是可以在释放第一个字符之前键入下一个字符。

Hope this helps. 希望这可以帮助。


Update : 更新:

You don't need any loop here, you can use substr to cut your string and insert the separator - and use maxlength to define the max number of charaters you want to contain your serial : 您在这里不需要任何循环,可以使用substr剪切字符串并插入分隔符-并使用maxlength定义要包含序列号的最大字符数:

 $('#serial').on('input', function() { var input_value = $(this).val().trim().replace(/-/g, ""); if(input_value.length > 3){ input_value = input_value.substr(0, 3) + '-' + input_value.substr(3); } if (input_value.length > 7){ input_value = input_value.substr(0, 7) + '-' + input_value.substr(7); } $(this).val(input_value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="serial" name="serial" maxlength="11"> 

try this code , replaced inefficient regex with substring 试试这个代码 ,用子字符串替换低效的正则表达式

$('#serial').input(function(){

  $(this).val( $(this).val().trim() );

  var str = $(this).val().trim().split( "-" ).join("");
  var output = "";
  while ( str.length > 3 )
  {
   output += str.substring(0,3) + "-";
   str = str.substring(3);
  }
  output += str;

  $(this).val(output);

});

A little further optimization 进一步优化

$('#serial').on('input', function() {

  var str = $(this).val().trim();
  var i = 3;
  while (i < str.length) {
    if (str.charAt(i) == '-') {
      i += 4;
    } else {
      str = str.slice(0, i) + '-' + str.charAt(str.length-1);
    }

  }
  $(this).val(str);

});

jsfiddle https://jsfiddle.net/h5hdvcwu/1/ jsfiddle https://jsfiddle.net/h5hdvcwu/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM