For example: 效果圖: ...
/** *textarea 字數限制 *obj textarea * maxlength 限制的最大字數 */ function textarealength(obj,maxlength){ var v = $(obj).val(); var l = v.length; if( l > maxlength){ v = v.substring(0,maxlength); $(obj).val(v); } $(obj).parent().find(".textarea-length").text(v.length); }
For example:
<style> .textarea-numberbar{ position:absolute; right:20px; bottom:5px; z-index:1; margin-bottom:0} </style>
<textarea name="desc" placeholder="請輸入內容" class="layui-textarea" lay-verify="required" onKeyUp="textarealength(this,150)"></textarea> <p class="textarea-numberbar"><em class="textarea-length">0</em>/150</p>
效果圖: