textarea-maxlength属性和字符串长度()不一致

textarea maxlength attrribute and string length() inconsistencies

本文关键字:不一致 属性 字符串 textarea-maxlength      更新时间:2023-09-26

尝试在文本区域下方写入剩余可用字符数。当我添加了很多换行符时,以下脚本无法按预期工作:

$textarea.keyup(function () {
  $div.text("Remaining characters: " + (parseInt($textarea.attr("maxlength"), 10) - $textarea.val().length));
})

当有很多换行符时,当我不能写任何东西时,脚本会报告更多可用字符。尝试用just替换''r''n,然后计算字符串长度,但没有成功。

怎么了?:)

JS FIDDLE链接

编辑:只有Chrome(最新版本--27.0.1453.116)无法按预期工作

您可以使用这个jQuery插件:jQuery MaxLength以这种方式配置它:

$('$textarea').maxlength({
    alwaysShow: true,
    preText: 'Remaining characters: ',
    postText: ' '
});