• 豌豆资源网
  • 开引网企业服务
  • 服务外包网
  • input,textarea限制字数,实时绑定

    1.在input 或 textarea中加属性 

    maxlength="10"
    

      

     

     2.js判断

    function limitImport(str,num){
        $(document).on('input propertychange',str,function(){
            var self = $(this);
            var content = self.val();
            if (content.length > num){
                self.val(content.substring(0,num));
            } 
            self.siblings('span').text(self.val().length+'/'+num);
        });
    }
    

      

     

    //用法示例 

    limitImport(‘.pushTe’,50);
    

    素材公社https://www.wode007.com/sites/73209.html 爱图网https://www.wode007.com/sites/73208.html

    没有什么特殊需要感觉还得第一个实用些 

     

    拓展: 实时绑定功能 

    $(document).bind('input propertychange','.class',function(){
        // console.log($(this).val().length)
        if($(this).val().length<=20){
            document.getElementById('tiCont').innerhtml = $(this).val().length;
        }
    });
    

      

    二种输入标签的实时绑定方式 。 需求:框后面有显示字数  

     

    posted @ 2020-07-19 17:34  前端一点红  阅读(383)  评论(0)    收藏  举报
  • 乐游资源网
  • 热爱资源网
  • 灵活用工代发薪平台
  • 企服知识
  • 355软件知识