首页 > 其他 > 详细

input,textarea限制字数,实时绑定

时间:2020-07-19 18:48:54      阅读:93      评论:0      收藏:0      [点我收藏+]

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

  

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

 

input,textarea限制字数,实时绑定

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!