How do you get the length of a string?

2019-01-30 00:48发布

问题:

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

回答1:

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



回答2:

The easiest way:

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


回答3:

jQuery is a JavaScript library.

You don't need to use jQuery to get the length of a string because it is a basic JavaScript string object property.

somestring.length;


回答4:

You don't need to use jquery.

var myString = 'abc';
var n = myString.length;

n will be 3.



回答5:

HTML

<div class="selector">Text mates</div>

SCRIPT

alert(jQuery('.selector').text().length);

RESULT

10



回答6:

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;


回答7:

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

alert(str.length);


回答8:

same way you do it in javascript:

"something".length



回答9:

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;