$(function () {
/**
* 字數限制
*/
$('[max-word-length]').each(function () {
var area = $(this);
var length = area.attr("max-word-length");
area.bind('input propertychange', function() {
var val = $(this).val();
if (val && val.length>length){
$(this).val( val.toString().substring(0,length) );
}
});
});
});
標籤內使用max-word-length="10" ,屬性即可,當前字數限制是10字,可以改變 :
<div style="margin: 10px;">
<textarea name="reason" cols="50" rows="5" placeholder="10字以內" max-word-length="10"></textarea>
</div>