JS與jQuery判斷文本框還剩多少字符可以輸入的方法
本文實例講述了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實現(xiàn)Javascript統(tǒng)計和限制字符個數(shù)功能
使用jQuery插件textlimit可以實現(xiàn)統(tǒng)計和限制字符個數(shù)功能,可應(yīng)用于文本框與文本區(qū)域,當(dāng)輸入文字時textlimit插件會及時統(tǒng)計當(dāng)前文本框與文本區(qū)域中的字符個數(shù),如果達到限制數(shù)則不允許輸入,同時可設(shè)置字符刪除速度,
使用實例
一、包含文件部分
<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>
當(dāng)在ID為test的文本框中輸入文字時,textlimit插件統(tǒng)計當(dāng)前輸入字符個數(shù)并顯示在一個span的元素中,如上效果圖,textlimit接口如下:
textlimit(counter_el, thelimit, speed)
接口參數(shù)說明:
- counter_el表示顯示當(dāng)前統(tǒng)計個數(shù)的選擇器標(biāo)簽,如:span
- thelimit表示限制個數(shù),也就是最多可輸入的個數(shù),如:256
- speed表示刪除字符速度,默認為15,注意,如果不需要可設(shè)置為-1,但不能是0
注意:英文字符與漢字字符都統(tǒng)計為一個字符
textlimit插件統(tǒng)計和限制字符數(shù)非常簡單,具體大家可以看看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);
PS:這里再為大家推薦兩款相關(guān)在線工具供大家參考:
字數(shù)統(tǒng)計工具:
http://tools.jb51.net/code/zishutongji
在線字符統(tǒng)計與編輯工具:
http://tools.jb51.net/code/char_tongji
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)學(xué)運算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript操作DOM技巧總結(jié)》及《JavaScript字符與字符串操作技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
前端無感知刷新token以及超時自動退出實現(xiàn)方案
前端需要做到無感刷新token,即刷token時要做到用戶無感知,避免頻繁登錄,下面這篇文章主要給大家介紹了關(guān)于前端無感知刷新token以及超時自動退出的實現(xiàn)方案,需要的朋友可以參考下2024-01-01JavaScript實現(xiàn)仿網(wǎng)易通行證表單驗證
這篇文章主要介紹了JavaScript實現(xiàn)仿網(wǎng)易通行證表單驗證,十分的實用,有需要的小伙伴可以參考下。2015-05-05js監(jiān)聽鼠標(biāo)點擊和鍵盤點擊事件并自動跳轉(zhuǎn)頁面
這篇文章主要介紹了js監(jiān)聽鼠標(biāo)點擊(onmousedown)和鍵盤點擊(onkeydown)事件并自動跳轉(zhuǎn)頁面,很簡單的一個實現(xiàn)2014-09-09關(guān)于layui toolbar和template的結(jié)合使用方法
今天小編就為大家分享一篇關(guān)于layui toolbar和template的結(jié)合使用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09