国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > JS > 正文

JS與jQuery判斷文本框還剩多少字符可以輸入的方法

2024-05-06 16:45:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了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>個(gè)字符  </td></tr>

jQuery插件textlimit實(shí)現(xiàn)Javascript統(tǒng)計(jì)和限制字符個(gè)數(shù)功能

使用jQuery插件textlimit可以實(shí)現(xiàn)統(tǒng)計(jì)和限制字符個(gè)數(shù)功能,可應(yīng)用于文本框與文本區(qū)域,當(dāng)輸入文字時(shí)textlimit插件會(huì)及時(shí)統(tǒng)計(jì)當(dāng)前文本框與文本區(qū)域中的字符個(gè)數(shù),如果達(dá)到限制數(shù)則不允許輸入,同時(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的文本框中輸入文字時(shí),textlimit插件統(tǒng)計(jì)當(dāng)前輸入字符個(gè)數(shù)并顯示在一個(gè)span的元素中,如上效果圖,textlimit接口如下:

textlimit(counter_el, thelimit, speed)

接口參數(shù)說明:

  • counter_el表示顯示當(dāng)前統(tǒng)計(jì)個(gè)數(shù)的選擇器標(biāo)簽,如:span
  • thelimit表示限制個(gè)數(shù),也就是最多可輸入的個(gè)數(shù),如:256
  • speed表示刪除字符速度,默認(rèn)為15,注意,如果不需要可設(shè)置為-1,但不能是0

注意:英文字符與漢字字符都統(tǒng)計(jì)為一個(gè)字符

textlimit插件統(tǒng)計(jì)和限制字符數(shù)非常簡(jiǎn)單,具體大家可以看看textlimit的庫(kù)文件,非常值得推薦。

/* * 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);

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 陇南市| 象山县| 湖州市| 大足县| 三门县| 芜湖市| 靖远县| 伽师县| 大渡口区| 临邑县| 西青区| 大足县| 金沙县| 南投市| 西峡县| 炎陵县| 兴业县| 仁寿县| 阜康市| 濮阳县| 华池县| 宝清县| 定边县| 微山县| 陇川县| 鄯善县| 淮阳县| 乳源| 曲沃县| 茶陵县| 柳州市| 额敏县| 深圳市| 拉萨市| 荥阳市| 扶沟县| 开平市| 梁平县| 北流市| 富顺县| 宁强县|