前台input输入框,输入内容并同步增加输入框长度

  $(document).ready(function() {                        
    $('#inputId').bind('input propertychange',function(){
      var len=getByteLen($(this).val());
      var width = Number(len)*8;
      if(width<100){    //100:初始设置的输入框长度
        $(this).css('width','100px');
      }else{
        $(this).css('width',width+'px');
      }
    });     
  })

  function getByteLen(val) { var len = 0; for (var i = 0; i < val.length; i++) { var strVal=val.charAt(i);
      //len所取数值,根据页面字体大小设置 并非死值       if(/[^x00-xff]/g.test(strVal)){         len += 1.8;//汉字       }else if(/^[0-9]+$/.test(strVal) || /^[a-zA-Z]+$/.test(strVal)){         len += 0.98;//数字 或 字母       }else{         len += 0.6;//处理特殊字符       }     }     return len;   }
原文地址:https://www.cnblogs.com/ljc1212/p/11660222.html