gpt4 book ai didi

jquery - 使用 jQuery 进行字符计数

转载 作者:行者123 更新时间:2023-12-03 21:29:29 24 4
gpt4 key购买 nike

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

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

仅计算最多 3 个字符空格,但不计算字符数。

最佳答案

对于包括空格的长度:

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

对于没有空格的长度:

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

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

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

例如,字符串"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

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

关于jquery - 使用 jQuery 进行字符计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com