JS与jQuery判断文本框还剩多少字符可以输入的方法
作者:gundumw100 时间:2024-04-25 13:07:20
本文实例讲述了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);
希望本文所述对大家JavaScript程序设计有所帮助。
来源:https://blog.csdn.net/gundumw100/article/details/69953103
标签:JS,jQuery,文本框,字符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL获取系统性能和状态代码
2024-01-13 10:56:18
Python数据分析之 Matplotlib 散点图绘制
2021-09-19 07:05:46
![](https://img.aspxhome.com/file/2023/8/124228_0s.webp)
SQL Server DBA维护常用语句
2024-01-22 01:47:00
javascript 读取图片文件的大小
2024-04-17 10:40:11
python利用tkinter实现屏保
2022-01-26 05:39:51
Python 中字符串拼接的多种方法
2023-09-25 07:02:13
python 内置模块详解
2023-09-08 00:55:55
10个精致的导航菜单欣赏及点评
2011-09-22 20:33:44
ASP 包含文件中的路径问题和使用单一数据库连接文件的解决方案
2011-04-07 10:35:00
MySQL Workbench安装及使用详解
2024-01-27 10:52:15
![](https://img.aspxhome.com/file/2023/8/105208_0s.png)
python pyg2plot的原理知识点总结
2021-03-27 23:13:51
如何使用Script Encoder?
2010-06-05 12:42:00
Django模板语言 Tags使用详解
2022-09-27 23:37:35
Django ORM F对象和Q对象查询
2023-10-23 15:33:04
![](https://img.aspxhome.com/file/2023/5/133845_0s.png)
js阻止浏览器默认行为的简单实例
2024-04-27 15:22:55
垂直无缝滚动图片(兼容性好)实例教程源码下载
2010-04-06 12:16:00
![](https://img.aspxhome.com/file/UploadPic/20104/6/scrollnews-65s.jpg)
Python文件和流(实例讲解)
2023-08-02 19:11:45
Git如何合并多次提交
2023-03-27 13:05:03
![](https://img.aspxhome.com/file/2023/3/132353_0s.png)
pytest allure添加环境信息实例讲解
2023-07-19 17:51:45
![](https://img.aspxhome.com/file/2023/8/89098_0s.png)
在VSCode中搭建Python开发环境并进行调试
2021-09-20 23:19:13
![](https://img.aspxhome.com/file/2023/6/114676_0s.png)