文字框的字數限制功能jquery外掛程式_jquery

來源:互聯網
上載者:User
後來在網上閑逛的時候突然發現一個比較好的文字框的字數限制的jquery plugin,簡單好用
效果如下圖

複製代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>關於文字框的字數限制功能</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="maxlength.js"></script>
</head>
<body>
<p>Enter your hobbies (<50 characters)</p>
<textarea style="width:300px; height:150px" data-maxsize="50" data-output="status1" wrap="virtual"></textarea><br />
<div id="status1" style="width:300px;font-weight:bold;text-align:right"></div><br />
<p>Enter your postal code (<6 characters)</p>
<input type="text" name="T1" size="15" data-maxsize="6">
</body>
</html>

屬性解釋:

data-maxsize:該文本域能夠接受的最大字數.(important)
data-output:如果想即時動態顯示文字框裡的字數,該屬性設定顯示字數的span或div的id.(字數顯示的顏色會隨著數量變化)
如果不修改它的源檔案的話,加添了js引用後,你只需要給自己的text或者textarea加上data-maxsize屬性就可以了。
關於動態設定最大值
使用setformfieldsize方法:
setformfieldsize($field, maxsize, outputfieldid)
屬性解釋:
$field:就是被限制的文字框的JQuery對象(參考Jquery文檔).
maxsize:和data-maxsize一樣,該文本域能夠接受的最大字數.
outputfieldid:和data-output一樣.
例子:
複製代碼 代碼如下:

<script type="text/javascript">
//jQuery(document).ready(function($){ //fire on DOM ready
setformfieldsize(jQuery('#comment'), 50, 'charsremain')
//});
</script>

在介面載入時執行.
示範代碼下載http://xiazai.jb51.net/200911/yuanma/textarea_maxlength.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.