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

查看:152
本文介绍了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。
完成后,离开textarea,js代码也会计算字符。



所以在我的情况下,我只能在chrome之前输入7个字符(包括空格)阻止了我。虽然maxlength属性的值是10: b
$ b

解决方案



  1 \r\\\

1\r\
1\r\ $
1

但是,javascript似乎只有一个 \r\\\
(我不确定哪一个)只加到 7


Here is an example:

$(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>

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

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

解决方案

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

1\r\n
1\r\n
1\r\n
1

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

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

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