之前也介紹過一個類似效果的JQuery插件jQuery maxlength文本字數限制插件,不過這次的charcount部署更簡單,而且有超出數字提示的功能.
簡單的部署: 1.載入js: 代碼如下:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/charCount.js"></script>
2.Html結構: 代碼如下:
<form id="form" method="post" action="">
<h2>Default Usage</h2>
<div>
<label for="message">Type your message</label>
<textarea id="message1" name="message1"></textarea>
</div>
<h2>Custom Usage</h2>
<div>
<label for="message">Another message (limited to 30, warning at 10)</label>
<textarea id="message2" name="message2"></textarea>
</div>
</form>
3.charCount插件設置: 代碼如下:
<script type="text/javascript">
$(document).ready(function(){
//default usage
$("#message1").charCount();
//custom usage
$("#message2").charCount({
allowed: 30,
warning: 10,
counterText: '剩余字數: '
});
});
</script>
演示代碼
打包打包下載