JS与jQuery判断文本框还剩多少字符可以输入的方法


Posted in jQuery onSeptember 01, 2018

本文实例讲述了JS与jQuery判断文本框还剩多少字符可以输入的方法。分享给大家供大家参考,具体如下:

javascript部分:

function $(id) {
  return document.getElementById(id);
}
var maxLen=255;
function checkMaxInput(){
  if($("summary").value.length>maxLen){
    $("summary").value=$("summary").value.substring(0,maxLen);
  }else{
    $("leaves").innerHTML=maxLen-$("summary").value.length;
  }
}

HTML部分:

<tr>
 <td>摘要:</td>
 <td>
  <html:textarea property="summary" rows="5" cols="60" onkeyup="checkMaxInput()"/>
  <br>
   还可以输入<span class="red" id="leaves">255</span>个字符
  </td>
</tr>

jQuery插件textlimit实现Javascript统计和限制字符个数功能

使用jQuery插件textlimit可以实现统计和限制字符个数功能,可应用于文本框与文本区域,当输入文字时textlimit插件会及时统计当前文本框与文本区域中的字符个数,如果达到限制数则不允许输入,同时可设置字符删除速度,

使用实例

一、包含文件部分

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="textlimit.js"></script>

二、HTML部分

<input type="text" name="test" value="" id="test" /><span>20</span>/256

三、Javascript部分

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#test").textlimit("span",256);
});
</script>

当在ID为test的文本框中输入文字时,textlimit插件统计当前输入字符个数并显示在一个span的元素中,如上效果图,textlimit接口如下:

textlimit(counter_el, thelimit, speed)

接口参数说明:

  • counter_el表示显示当前统计个数的选择器标签,如:span
  • thelimit表示限制个数,也就是最多可输入的个数,如:256
  • speed表示删除字符速度,默认为15,注意,如果不需要可设置为-1,但不能是0

注意:英文字符与汉字字符都统计为一个字符

textlimit插件统计和限制字符数非常简单,具体大家可以看看textlimit的库文件,非常值得推荐。

/*
 * TextLimit - jQuery plugin for counting and limiting characters for input and textarea fields
 *
 * pass '-1' as speed if you don't want the char-deletion effect. (don't just put 0)
 * Example: jQuery("Textarea").textlimit('span.counter',256)
 *
 * $Version: 2009.07.25 +r2
 * Copyright (c) 2009 Yair Even-Or
 * vsync.design@gmail.com
*/
(function(jQuery) {
  jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
    var charDelSpeed = speed || 15;
    var toggleCharDel = speed != -1;
    var toggleTrim = true;
    var that = this[0];
    var isCtrl = false;
    updateCounter();
    function updateCounter(){
      if(typeof that == "object")
        jQuery(counter_el).text(thelimit - that.value.length+" characters remaining");
    };
    this.keydown (function(e){
      if(e.which == 17) isCtrl = true;
      var ctrl_a = (e.which == 65 && isCtrl == true) ? true : false; // detect and allow CTRL + A selects all.
      var ctrl_v = (e.which == 86 && isCtrl == true) ? true : false; // detect and allow CTRL + V paste.
      // 8 is 'backspace' and 46 is 'delete'
      if( this.value.length >= thelimit && e.which != '8' && e.which != '46' && ctrl_a == false && ctrl_v == false)
        e.preventDefault();
    })
    .keyup (function(e){
      updateCounter();
      if(e.which == 17)
        isCtrl=false;
      if( this.value.length >= thelimit && toggleTrim ){
        if(toggleCharDel){
          // first, trim the text a bit so the char trimming won't take forever
          // Also check if there are more than 10 extra chars, then trim. just in case.
          if ( (this.value.length - thelimit) > 10 )
            that.value = that.value.substr(0,thelimit+100);
          var init = setInterval
            (
              function(){
                if( that.value.length <= thelimit ){
                  init = clearInterval(init); updateCounter()
                }
                else{
                  // deleting extra chars (one by one)
                  that.value = that.value.substring(0,that.value.length-1); jQuery(counter_el).text('Trimming... '+(thelimit - that.value.length));
                }
              } ,charDelSpeed
            );
        }
        else this.value = that.value.substr(0,thelimit);
      }
    });
  };
})(jQuery);
jQuery 相关文章推荐
Vue.js列表渲染绑定jQuery插件的正确姿势
Jun 29 jQuery
详解jQuery中的isPlainObject()使用方法
Feb 27 jQuery
jQuery实现获取选中复选框的值实例详解
Jun 28 jQuery
使用jQuery给Table动态增加行、清空table的方法
Sep 05 jQuery
jquery+php后台实现省市区联动功能示例
May 23 jQuery
jQuery事件委托代码实践详解
Jun 21 jQuery
jQuery Ajax async=&gt;false异步改为同步时,解决导致浏览器假死的问题
Jul 22 jQuery
jquery向后台提交数组的代码分析
Feb 20 jQuery
viewer.js一个强大的基于jQuery的图像查看插件(支持旋转、缩放)
Apr 01 jQuery
jQuery实现的上拉刷新功能组件示例
May 01 jQuery
基于jquery实现彩色投票进度条代码解析
Aug 26 jQuery
jQuery实现可以计算进制转换的计算器
Oct 19 jQuery
jQuery解析json格式数据示例
Sep 01 #jQuery
jQuery实现表格隔行换色
Sep 01 #jQuery
基于jQuery ztree实现表格风格的树状结构
Aug 31 #jQuery
解决jQuery使用append添加的元素事件无效的问题
Aug 30 #jQuery
jQuery实现的简单手风琴效果示例
Aug 29 #jQuery
jQuery实现的淡入淡出图片轮播效果示例
Aug 29 #jQuery
jQuery实现的响应鼠标移动方向插件用法示例【附源码下载】
Aug 28 #jQuery
You might like
php实现快速排序法函数代码
2012/08/27 PHP
PHP数组排序之sort、asort与ksort用法实例
2014/09/08 PHP
浅析Yii2中GridView常见操作
2016/04/22 PHP
php编译安装php-amq扩展简明教程
2016/06/25 PHP
浅谈PHP中静态方法和非静态方法的相互调用
2016/10/04 PHP
PHP用正则匹配form表单中所有元素的类型和属性值实例代码
2017/02/28 PHP
Jquery中显示隐藏的实现代码分析
2011/07/26 Javascript
基于jQuery的360图片展示实现代码
2012/06/14 Javascript
JS检测图片大小的实例
2013/08/21 Javascript
超实用的JavaScript表单代码段
2016/02/26 Javascript
Uploadify上传文件方法
2016/03/16 Javascript
基于Bootstrap使用jQuery实现简单可编辑表格
2016/05/04 Javascript
JS 通过系统时间限定动态添加 select option的实例代码
2016/06/09 Javascript
js修改onclick动作的四种方法(推荐)
2016/08/18 Javascript
Vue.js学习之计算属性
2017/01/22 Javascript
在js中做数字字符串补0(js补零)
2017/03/25 Javascript
Angular4 ElementRef的应用
2018/02/26 Javascript
微信小程序的mpvue框架快速上手指南
2019/05/15 Javascript
微信小程序批量上传图片到七牛(推荐)
2019/12/19 Javascript
[49:41]NB vs NAVI Supermajor小组赛A组 BO3 第一场 6.2
2018/06/03 DOTA
将Python的Django框架与认证系统整合的方法
2015/07/24 Python
python代码编写计算器小程序
2020/03/30 Python
python requests使用socks5的例子
2019/07/25 Python
python+selenium定时爬取丁香园的新型冠状病毒数据并制作出类似的地图(部署到云服务器)
2020/02/09 Python
Python实现手绘图效果实例分享
2020/07/22 Python
CSS3 :not()选择器实现最后一行li去除某种css样式
2016/10/19 HTML / CSS
百度JavaScript笔试题
2015/01/15 面试题
质检的岗位职责
2013/11/17 职场文书
股权投资意向书
2014/04/01 职场文书
节能宣传周活动总结
2014/05/08 职场文书
企业口号大全
2014/06/12 职场文书
烟台的海导游词
2015/02/02 职场文书
2015年元宵节活动总结
2015/02/06 职场文书
鉴史问廉观后感
2015/06/10 职场文书
《鲁班学艺》读后感3篇
2019/11/27 职场文书
SpringDataJPA实体类关系映射配置方式
2021/12/06 Java/Android