基于jquery的textarea發(fā)布框限制文字字?jǐn)?shù)輸入(添加中文識別)
更新時間:2012年02月16日 18:58:06 作者:
基于jquery的textarea發(fā)布框限制文字字?jǐn)?shù)輸入(添加中文識別),需要的朋友可以參考下
效果圖如下 :
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("textarea").keyup();
});
function strLenCalc(obj, checklen, maxlen) {
var v = obj.val(), charlen = 0, maxlen = !maxlen ? 200 : maxlen, curlen = maxlen, len = v.length;
for(var i = 0; i < v.length; i++) {
if(v.charCodeAt(i) < 0 || v.charCodeAt(i) > 255) {
curlen -= 1;
}
}
if(curlen >= len) {
$("#"+checklen).html("還可輸入 <strong>"+Math.floor((curlen-len)/2)+"</strong> 個字").css('color', '');
$("#subBtn").removeAttr("disabled");
} else {
$("#"+checklen).html("已經(jīng)超過 <strong>"+Math.ceil((len-curlen)/2)+"</strong> 個字").css('color', '#FF0000');
$("#subBtn").attr("disabled", "disabled");
}
}
</script>
<textarea tabindex="11" onkeyup="strLenCalc($(this), 'checklen', 80);" value="" class="px" name="subject" id="subject"></textarea>
<span id="checklen">還可輸入 <strong>40</strong> 個漢字</span>
<input type="submit" id="subBtn" value="發(fā)布" />
源代碼如下 :
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("textarea").keyup();
});
function strLenCalc(obj, checklen, maxlen) {
var v = obj.val(), charlen = 0, maxlen = !maxlen ? 200 : maxlen, curlen = maxlen, len = v.length;
for(var i = 0; i < v.length; i++) {
if(v.charCodeAt(i) < 0 || v.charCodeAt(i) > 255) {
curlen -= 1;
}
}
if(curlen >= len) {
$("#"+checklen).html("還可輸入 <strong>"+Math.floor((curlen-len)/2)+"</strong> 個字").css('color', '');
$("#subBtn").removeAttr("disabled");
} else {
$("#"+checklen).html("已經(jīng)超過 <strong>"+Math.ceil((len-curlen)/2)+"</strong> 個字").css('color', '#FF0000');
$("#subBtn").attr("disabled", "disabled");
}
}
</script>
<textarea tabindex="11" onkeyup="strLenCalc($(this), 'checklen', 80);" value="" class="px" name="subject" id="subject"></textarea>
<span id="checklen">還可輸入 <strong>40</strong> 個漢字</span>
<input type="submit" id="subBtn" value="發(fā)布" />
您可能感興趣的文章:
- jquery清空textarea等輸入框?qū)崿F(xiàn)代碼
- jQuery textarea的長度進(jìn)行驗(yàn)證
- JQuery為textarea添加maxlength屬性的代碼
- jquery實(shí)現(xiàn)textarea輸入框限制字?jǐn)?shù)的方法
- textarea中的手動換行處理的jquery代碼
- 使用jquery給input和textarea設(shè)定ie中的focus
- Jquery實(shí)現(xiàn)點(diǎn)擊按鈕,連續(xù)地向textarea中添加值的實(shí)例代碼
- jQuery替換textarea中換行的方法
- TextArea不支持maxlength的解決辦法(jquery)
- jquery實(shí)現(xiàn)限制textarea輸入字?jǐn)?shù)的方法
相關(guān)文章
利用JQUERY實(shí)現(xiàn)多個AJAX請求等待的實(shí)例
下面小編就為大家分享一篇利用JQUERY實(shí)現(xiàn)多個AJAX請求等待的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12jQuery使用$獲取對象后檢查該對象是否存在的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨Query使用$獲取對象后檢查該對象是否存在的實(shí)現(xiàn)方法。小編覺而挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09jQuery實(shí)現(xiàn)延遲跳轉(zhuǎn)的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)延遲跳轉(zhuǎn)的方法,涉及jQuery中setTimeout方法延遲觸發(fā)的使用技巧,需要的朋友可以參考下2015-06-06jQuery修改li下的樣式以及l(fā)i下的img的src的值的方法
這篇文章主要為大家介紹了jQuery如何修改li下的樣式,以及修改li下的img的src的值,示例代碼很簡單,一看就會2014-11-11基于jQuery實(shí)現(xiàn)的美觀星級評論打分組件代碼
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)的美觀星級評論打分組件代碼,涉及jQuery回調(diào)函數(shù)及頁面元素屬性動態(tài)操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10兼容主流瀏覽器的jQuery+CSS 實(shí)現(xiàn)遮罩層的簡單代碼
比起使用注冊頁和登陸頁,網(wǎng)站在當(dāng)前頁使用遮罩層注冊和登陸的用戶體驗(yàn)要好不少。這里使用jQuery和CSS實(shí)現(xiàn)一個簡單的遮罩效果。2014-10-10Jquery實(shí)現(xiàn)的table最后一行添加樣式的代碼
有時間需要將表格的最后一行加上樣式,方便閱讀等需要,需要的朋友可以參考下。2010-05-05