使用jquery进行字符计数

use*_*265 35 jquery

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

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

只计算3个字符空格,但不计算字符数

Rio*_*ams 104

包括空白在内的长度:

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

对于没有空格的长度:

$("#id").val().replace(/ /g,'').length
Run Code Online (Sandbox Code Playgroud)

仅用于删除开始和尾随空格:

$.trim($("#test").val()).length
Run Code Online (Sandbox Code Playgroud)

例如,字符串" t e s t "将评估为:

//" 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
Run Code Online (Sandbox Code Playgroud)

是一个使用所有这些的演示.