当您添加属性“maxlength”时,这是 Jquery 为 textarea 强制执行 maxlength。IE 不支持 Maxlength。如何调整我的代码以处理页面上的多个文本区域?目前,如果我将文本添加到任何文本区域,它们都会以相同的值倒计时。
查询:
$(document).ready( function () {
maxLength = $("textarea").attr("maxlength");
$("textarea").after("<div><span id='remainingLengthTempId'>"
+ maxLength + "</span> remaining</div>");
$("textarea").bind("keyup change", function(){checkMaxLength(this.id, maxLength); } )
});
function checkMaxLength(textareaID, maxLength){
currentLengthInTextarea = $("#"+textareaID).val().length;
$(remainingLengthTempId).text(parseInt(maxLength) - parseInt(currentLengthInTextarea));
if (currentLengthInTextarea > (maxLength)) {
// Trim the field current length over the maxlength.
$("textarea").val($("textarea").val().slice(0, maxLength));
$(remainingLengthTempId).text(0);
}
}
html:
Skills:
<textarea id="1" maxlength="200" rows="10" cols="60" ></textarea>
Postions:
<textarea id="2" maxlength="200" rows="10" cols="60" ></textarea>
Comments
<textarea id="3" maxlength="100" rows="10" cols="60" ></textarea>