可用js自带的substring函数截取需要的字符串:
1.public String substring(int beginIndex)
<td> <textarea id="templateContent" name="templateContent" rows="3" cols="48" maxlength="200" onKeyDown="textCounter()" onkeyup="textCounter()"></textarea> <p style="color:#888;margin:5px 0px;">短信字数限制为<b class="color-red">200</b>个字,还可以输入<b id="remLen" class="color-red">200</b>个字</p> </td>
//提示文本区内可输入字数 function textCounter() { if ($("#templateContent").val().length > 200) //如果元素区字符数大于最大字符数,按照最大字符数截断; $("#templateContent").val($("#templateContent").val().substring(0, 200)); else //在记数区文本框内显示剩余的字符数; $("#remLen").text( 200 - $("#templateContent").val().length); }
原文:https://www.cnblogs.com/xuhk1819/p/12201616.html