使用jQuery的字符计数 [英] character count using jquery

查看:41
本文介绍了使用jQuery的字符计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery计算文本框中的字符数?

How can i count the number of characters in a textbox using jquery?

$("#id").val().length < 3

最多可计算3个字符空间,但不包括字符数

just counts upto 3 character spaces but not the number of characters

推荐答案

包括空格的长度:

$("#id").val().length

对于没有空格的长度:

$("#id").val().replace(/ /g,'').length

仅删除开头和结尾的空格:

$.trim($("#test").val()).length

例如,字符串" t e s t "的计算结果为:

For example, the string " t e s t " would evaluate as:

//" t e s t "
$("#id").val(); 

//Example 1
$("#id").val().length; //Returns 9
//Example 2
$("#id").val().replace(/ /g,'').length; //Returns 4
//Example 3
$.trim($("#test").val()).length; //Returns 7

此处是使用所有示例的演示.

Here is a demo using all of them.

这篇关于使用jQuery的字符计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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