Chrome 使用 maxlength 属性计算 textarea 中的字符错误 [英] Chrome counts characters wrong in textarea with maxlength attribute

查看:30
本文介绍了Chrome 使用 maxlength 属性计算 textarea 中的字符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子:

$(function() {
  $('#test').change(function() {
    $('#length').html($('#test').val().length)
  })
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id=test maxlength=10></textarea>
length = <span id=length>0</span>

用行(一行一个字符)填充文本区域,直到浏览器允许.完成后离开textarea,js代码也会计算字符.

Fill textarea with lines (one character at one line) until browser allows. When you finish, leave textarea, and js code will calculate characters too.

所以就我而言,在 chrome 阻止我之前,我只能输入 7 个字符(包括空格).虽然 maxlength 属性的值为 10:

So in my case I could enter only 7 characters (including whitespaces) before chrome stopped me. Although value of maxlength attribute is 10:

推荐答案

当涉及到 maxlength 时,您的每个回车符被视为 2 个字符.

Your carriage returns are considered 2 characters each when it comes to maxlength.

1

1

1

1

但似乎 javascript 只能使用 之一(我不确定是哪一个),它加起来只有 7.

But it seems that the javascript only could one of the (I am not sure which one) which only adds up to 7.

这篇关于Chrome 使用 maxlength 属性计算 textarea 中的字符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆