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异步提交表单实例
May 30 jQuery
jquery实现限制textarea输入字数的方法
Sep 06 jQuery
jquery实现左右轮播图效果
Sep 28 jQuery
简单实现jQuery弹窗效果
Oct 30 jQuery
jQuery实现鼠标响应式透明度渐变动画效果示例
Feb 13 jQuery
jQuery中$原理实例分析
Aug 13 jQuery
jQuery操作attr、prop、val()/text()/html()、class属性
May 23 jQuery
jquery实现弹窗(系统提示框)效果
Dec 10 jQuery
jQuery实现简单三级联动效果
Sep 05 jQuery
jQuery实现动态操作table行
Nov 23 jQuery
jquery插件实现代码雨特效
Apr 24 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中的时间处理
2006/10/09 PHP
在PHP3中实现SESSION的功能(三)
2006/10/09 PHP
php使用正则表达式获取图片url的方法
2015/01/16 PHP
thinkphp实现图片上传功能
2016/01/13 PHP
php版阿里大于(阿里大鱼)短信发送实例详解
2016/11/30 PHP
JavaScript 读取元素的CSS信息的代码
2010/02/07 Javascript
ASP.NET jQuery 实例10 动态修改hyperlink的URL值
2012/02/03 Javascript
js中创建对象的几种方式
2017/02/05 Javascript
Angularjs 动态添加指令并绑定事件的方法
2017/04/13 Javascript
vue学习笔记之vue1.0和vue2.0的区别介绍
2017/05/17 Javascript
详解如何在微信小程序中愉快地使用sass
2018/07/30 Javascript
JS实现可切换图片的幻灯切换效果示例
2019/05/24 Javascript
初学node.js中实现删除用户路由
2019/05/27 Javascript
Windows8下安装Python的BeautifulSoup
2015/01/22 Python
python异常和文件处理机制详解
2016/07/19 Python
python批量读取txt文件为DataFrame的方法
2018/04/03 Python
解决Django migrate No changes detected 不能创建表的问题
2018/05/27 Python
python 文件转成16进制数组的实例
2018/07/09 Python
解决python中遇到字典里key值为None的情况,取不出来的问题
2018/10/17 Python
python批量修改ssh密码的实现
2019/08/08 Python
pytorch 输出中间层特征的实例
2019/08/17 Python
python几种常用功能实现代码实例
2019/12/25 Python
Python Pillow.Image 图像保存和参数选择方式
2020/01/09 Python
使用Python脚本从文件读取数据代码实例
2020/01/19 Python
python字符串,元组,列表,字典互转代码实例详解
2020/02/14 Python
Python smtp邮件发送模块用法教程
2020/06/15 Python
Python txt文件如何转换成字典
2020/11/03 Python
英国礼品和生活方式品牌:Treat Republic
2020/11/21 全球购物
高一历史教学反思
2014/01/13 职场文书
法人委托书的范本格式
2014/09/11 职场文书
物业管理委托协议(2篇)
2014/09/23 职场文书
计划生育个人总结
2015/03/02 职场文书
保卫工作个人总结
2015/03/03 职场文书
销售会议开幕词
2016/03/04 职场文书
python数据处理之Pandas类型转换
2022/04/28 Python
详解Go语言中Get/Post请求测试
2022/06/01 Golang