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插件FusionWidgets实现的Bulb图效果示例【附demo源码下载】
Mar 23 jQuery
jQuery 表单序列化实例代码
Jun 11 jQuery
jquery select插件异步实时搜索实例代码
Oct 20 jQuery
jQuery实现获取form表单内容及绑定数据到form表单操作分析
Jul 03 jQuery
jQuery动态生成的元素绑定事件操作实例分析
May 04 jQuery
jquery实现自定义树形表格的方法【自定义树形结构table】
Jul 12 jQuery
jQuery-Citys省市区三级菜单联动插件使用详解
Jul 26 jQuery
jQuery实现弹幕特效
Nov 29 jQuery
JQuery样式与属性设置方法分析
Dec 07 jQuery
jquery实现轮播图特效
Apr 12 jQuery
jQuery编写QQ简易聊天框
Aug 27 jQuery
jQuery实现tab栏切换效果
Dec 22 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 printf输出格式使用说明
2010/12/05 PHP
php set_time_limit()函数的使用详解
2013/06/05 PHP
解析php中如何调用用户自定义函数
2013/08/06 PHP
ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法
2014/11/04 PHP
php使用PDO获取结果集的方法
2017/02/16 PHP
php xhprof使用实例详解
2019/04/15 PHP
IE6/7 and IE8/9/10(IE7模式)依次隐藏具有absolute或relative的父元素和子元素后再显示父元素
2011/07/31 Javascript
跨浏览器通用、可重用的选项卡tab切换js代码
2011/09/20 Javascript
实现局部遮罩与关闭原理及代码
2013/02/04 Javascript
jQuery 删除或是清空某个HTML元素示例
2014/08/04 Javascript
js对象的复制继承实例
2015/01/10 Javascript
jQuery与Ajax以及序列化
2016/02/01 Javascript
浅谈JavaScript的push(),pop(),concat()方法
2016/06/03 Javascript
利用JS判断客户端类型你应该知道的四种方法
2017/12/22 Javascript
JS实现左边列表移到到右边列表功能
2018/03/28 Javascript
小程序云开发初探(小结)
2018/10/24 Javascript
vue双向绑定数据限制长度的方法
2019/11/04 Javascript
vue自定义树状结构图的实现方法
2020/10/18 Javascript
[02:39]DOTA2英雄基础教程 极限穿梭编织者
2013/12/05 DOTA
10款最好的Web开发的 Python 框架
2015/03/18 Python
python删除指定类型(或非指定)的文件实例详解
2015/07/06 Python
尝试使用Python多线程抓取代理服务器IP地址的示例
2015/11/09 Python
Python编写简单的HTML页面合并脚本
2016/07/11 Python
在java中如何定义一个抽象属性示例详解
2017/08/18 Python
Python数据预处理之数据规范化(归一化)示例
2019/01/08 Python
Python基础学习之函数方法实例详解
2019/06/18 Python
Pytorch的mean和std调查实例
2020/01/02 Python
基于Python计算圆周率pi代码实例
2020/03/25 Python
大学生专科毕业生自我评价
2013/11/17 职场文书
高级电工工作职责
2013/11/21 职场文书
竞争上岗演讲稿范文
2014/05/12 职场文书
关于开学的感想
2015/08/10 职场文书
JS Canvas接口和动画效果大全
2021/04/29 Javascript
欧元符号 €
2022/02/17 杂记
Windows Server 2019 配置远程控制以及管理方法
2022/04/28 Servers
windows server2012 R2下安装PaddleOCR服务的的详细步骤
2022/09/23 Servers