$(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>
本文介绍了一种在前端实现输入字段字数限制的方法,通过使用jQuery监听input和propertychange事件,当输入内容超过预设的最大长度时,自动截断超出部分,确保用户输入符合规定长度。
823

被折叠的 条评论
为什么被折叠?



