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 相关文章推荐
jQuery实现动态生成表格并为行绑定单击变色动作的方法
Apr 17 jQuery
jquery将标签元素的高设为屏幕的百分比
Apr 19 jQuery
jquery实现简单实用的轮播器
May 23 jQuery
vue单页应用中如何使用jquery的方法示例
Jul 27 jQuery
详解jquery选择器的原理
Aug 01 jQuery
jQuery实现右侧抽屉式在线客服功能
Dec 25 jQuery
jQuery+Cookie实现切换皮肤功能【附源码下载】
Mar 25 jQuery
jQuery 同时获取多个标签的指定内容并储存为数组
Nov 20 jQuery
jQuery实现上下滚动公告栏详细代码
Nov 21 jQuery
jquery实现上传图片功能
Jun 29 jQuery
jQuery列表动态增加和删除的实现方法
Nov 05 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中使用与Perl兼容的正则表达式
2006/11/26 PHP
PHP控制网页过期时间的代码
2008/09/28 PHP
浅析php与数据库代码开发规范
2013/08/08 PHP
浅谈thinkphp的实例化模型
2015/01/04 PHP
PHP中使用curl入门教程
2015/07/02 PHP
PHP实现的浏览器检查类
2016/04/11 PHP
php 使用 __call实现重载功能示例
2019/11/18 PHP
用JavaScript和注册表脚本实现右键收藏Web页选中文本
2007/01/28 Javascript
JavaScript 对象的属性和方法4种不同的类型
2010/03/19 Javascript
利用js(jquery)操作Cookie的方法说明
2013/12/19 Javascript
JQuery限制复选框checkbox可选中个数的方法
2015/04/20 Javascript
Jquery使用val方法读写value值
2015/05/18 Javascript
jQuery.prop() 使用详解
2015/07/19 Javascript
JavaScript 巧学巧用
2017/05/23 Javascript
Vue组件选项props实例详解
2017/08/18 Javascript
JavaScript基础之this和箭头函数详析
2019/09/05 Javascript
es6函数name属性功能与用法实例分析
2020/04/18 Javascript
工作中常用js功能汇总
2020/11/07 Javascript
python读写文件操作示例程序
2013/12/02 Python
利用Anaconda完美解决Python 2与python 3的共存问题
2017/05/25 Python
pyqt5 使用cv2 显示图片,摄像头的实例
2019/06/27 Python
Django实现web端tailf日志文件功能及实例详解
2019/07/28 Python
对YOLOv3模型调用时候的python接口详解
2019/08/26 Python
Python 去除字符串中指定字符串
2020/03/05 Python
Python操作Excel工作簿的示例代码(\*.xlsx)
2020/03/23 Python
公益活动策划方案
2014/01/09 职场文书
搞笑婚礼主持词
2014/03/13 职场文书
四年级学生评语大全
2014/04/21 职场文书
建筑工地标语
2014/06/18 职场文书
搞笑车尾标语
2014/06/23 职场文书
店铺转让协议书(2014版)
2014/09/23 职场文书
社区综治工作汇报
2014/10/27 职场文书
小学一年级学生评语大全
2014/12/25 职场文书
推广普通话宣传标语口号
2015/12/26 职场文书
青年岗位能手事迹材料(2016推荐版)
2016/03/01 职场文书
CSS3实现列表无限滚动/轮播效果
2021/06/23 HTML / CSS