How do you get the length of a string?

2019-01-30 01:18发布

How do you get the length of a string in jQuery?

9条回答
小情绪 Triste *
2楼-- · 2019-01-30 01:25

You don't need jquery, just use yourstring.length. See reference here and also here.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-30 01:25

In jQuery :

var len = jQuery('.selector').val().length; //or 
( var len = $('.selector').val().length;) //- If Element is Text Box

OR

var len = jQuery('.selector').html().length; //or
( var len = $('.selector').html().length; ) //- If Element is not Input Text Box

In JS :

var len = str.len;
查看更多
做自己的国王
4楼-- · 2019-01-30 01:28

same way you do it in javascript:

"something".length

查看更多
ら.Afraid
5楼-- · 2019-01-30 01:38

The easiest way:

$('#selector').val().length
查看更多
对你真心纯属浪费
6楼-- · 2019-01-30 01:38

It's not jquery you need, it's JS:

alert(str.length);
查看更多
疯言疯语
7楼-- · 2019-01-30 01:40

A somewhat important distinction is if the element is an input or not. If an input you can use:

$('#selector').val().length;

otherwise if the element is a different html element like a paragraph or list item div etc, you must use

$('#selector').text().length;
查看更多
登录 后发表回答