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;
    }
});

  

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

原文地址:https://www.cnblogs.com/ypppt/p/13340538.html