业务需求
最近做打印页面,输入字数过多影响打印效果,和客户商量后决定限制输入最大字数来保证避免样式修改
html代码
<td colspan="9"> <textarea cols="75" rows="11" style="border: none;" id="healthGuide1">${jmExaminationFeedback.healthGuide}</textarea> <div>您还可以输入 <span id="text-healthGuide1-count" style="color: red">0</span> /350个文字</div> </td>
js代码
$(function(){ $("#text-healthGuide1-count").text(350-$("#healthGuide1").val().length); //健康指导超出350字提示 $("#healthGuide1").on("input propertychange", function() { var $this = $(this), _val = $this.val(), count = ""; if (_val.length > 350) { $this.val(_val.substring(0, 350)); $("#text-healthGuide1-count").text(0) }else{ count =_val.length; $("#text-healthGuide1-count").text(350-count); } });
}