如何获取 jQuery 中多行 TextBox 条目的长度(包括回车符)?
我正在使用 jQuery“maxlength”函数(在这里找到 http:// /plugins.jquery.com/node/14014/release?api_version%5B%5D=10),但在将结果存储在数据库中时遇到问题。该函数仅使用 $(this).val().length
来获取已输入的字符数。
MaxLength 将限制输入的字符数,但不计算特殊字符(例如回车符)。因此,如果用户输入 4 行,每行末尾都有一个 CR,则提交表单时我们最终会得到 Max 个字符 + 4。因此,当我尝试保存到数据库时,我收到错误,因为字符串太长。
我可以简单地去掉 CR,但我想知道是否有某种方法可以将这些特殊字符包含在长度计数中,以便我得到真实的计数。
I am using the jQuery "maxlength" function (found here http://plugins.jquery.com/node/14014/release?api_version%5B%5D=10) but am running into an issue when storing the result in the database. This function simply uses $(this).val().length
to get the number of characters that have been entered.
MaxLength will cap the number of characters entered but it does not count special characters (e.g. the Carriage Return). So, if the user enters 4 lines with a CR at the end of each, we end up with Max characters + 4 when the form is submitted. Thus, when I try to save to the database, I get an error because the string is too long.
I could simply strip out the CRs but I am wondering if there is some method for including these special characters in the length count so I get a true count.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个: http://jsfiddle.net/UhQnu/1/
改编自:http://forums.digitalpoint.com/showthread.php?t=363964
Try this: http://jsfiddle.net/UhQnu/1/
Adapted from this: http://forums.digitalpoint.com/showthread.php?t=363964
如果您可以简单地去掉 CR,那么也许将结果字符串长度(与原始字符串长度相比)的差值添加到原始计数中,可以给出真实的计数?
If you can simply strip out the CRs, then perhaps adding the difference of the resulting string length (compared to origial string length) to the original count, could give a true count?