¿Cómo se obtiene la longitud de una cuerda?


¿Cómo se obtiene la longitud de una cadena en jQuery?

Author: Xufox, 2009-06-25

9 answers

No necesita jquery, solo use yourstring.length. Véase la referencia aquí y también aquí.

 420
Author: Artem Barger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-07 19:42:09

La forma más fácil:

$('#selector').val().length
 181
Author: Andrei Iarus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-18 16:31:43

JQuery es una biblioteca JavaScript.

No necesita usar jQuery para obtener la longitud de una cadena porque es una propiedad básica de objeto de cadena de JavaScript.

somestring.length;
 33
Author: andreialecu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-06-25 14:02:30

HTML

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

SCRIPT

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

RESULTADO

10

 20
Author: Joedel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-30 07:51:09

No necesitas usar jquery.

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

N será 3.

 19
Author: Lawrence Barsanti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-06-25 14:01:37

Una distinción algo importante es si el elemento es una entrada o no. Si una entrada se puede utilizar:

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

De lo contrario, si el elemento es un elemento html diferente, como un párrafo o un elemento de lista div, etc., debe usar

$('#selector').text().length;
 19
Author: A_funs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-16 18:22:19

No es jquery lo que necesitas, es JS:

alert(str.length);
 12
Author: karim79,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-06-25 14:00:41

De la misma manera que lo haces en javascript:

"something".length

 11
Author: mkoryak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-05 01:38:56

En jQuery:

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

O

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

En JS:

var len = str.len;
 1
Author: Mahak Choudhary,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-17 17:53:27