如何使用jQuery获得在文本框中输入的文本长度?

时间:2022-02-25 21:11:48

How can I get the length of text entered in a textbox using jQuery?

如何使用jQuery获得在文本框中输入的文本长度?

6 个解决方案

#1


303  

var myLength = $("#myTextbox").val().length;

#2


101  

If your textbox has an id attribute of "mytextbox", then you can get the length like this:

如果你的文本框有“textmybox”的id属性,那么你可以得到如下长度:

var myLength = $("#mytextbox").val().length;
  • $("#mytextbox") finds the textbox by its id.
  • $(“#mytextbox”)根据文本框的id查找文本框。
  • .val() gets the value of the input element entered by the user, which is a string.
  • .val()获取用户输入的输入元素的值,即字符串。
  • .length gets the number of characters in the string.
  • .length获取字符串中的字符数。

#3


9  

For me its not text box its a span tag and this worked for me.

对我来说,它不是文本框,它是一个span标签,这对我很有用。

var len = $("span").text().length;

#4


3  

Below mentioned code works perfectly fine for taking length of any characters entered in textbox.

下面的代码可以很好地处理文本框中输入的任何字符的长度。

$("#Texboxid").val().length;

$(" # Texboxid ").val . length();

#5


2  

CODE

$('#montant-total-prevu').on("change", function() {

var taille = $('#montant-total-prevu').val().length;

    if (taille > 9) {

//TODO

}

});

#6


0  

You need to only grab the element with an appropriate jQuery selector and then the .val() method to get the string contained in the input textbox and then call the .length on that string.

您只需使用适当的jQuery选择器获取元素,然后使用.val()方法获取输入文本框中包含的字符串,然后调用该字符串的.length。

$('input:text').val().length

$('输入:文本').val(). length

However, be warned that if the selector matches multiple inputs, .val() will only return the value of the first textbox. You can also change the selector to get a more specific element but keep the :text to ensure it's an input textbox.

但是,请注意,如果选择器匹配多个输入,.val()将只返回第一个文本框的值。您还可以更改选择器以获得更特定的元素,但保留:text以确保它是一个输入文本框。

On another note, to get the length of a string contained in another, non-input element, you can use the .text() function to get the string and then use .length on that string to find its length.

在另一个注释中,要获取另一个非输入元素中包含的字符串的长度,您可以使用.text()函数获取字符串,然后使用.length在该字符串上找到它的长度。

#1


303  

var myLength = $("#myTextbox").val().length;

#2


101  

If your textbox has an id attribute of "mytextbox", then you can get the length like this:

如果你的文本框有“textmybox”的id属性,那么你可以得到如下长度:

var myLength = $("#mytextbox").val().length;
  • $("#mytextbox") finds the textbox by its id.
  • $(“#mytextbox”)根据文本框的id查找文本框。
  • .val() gets the value of the input element entered by the user, which is a string.
  • .val()获取用户输入的输入元素的值,即字符串。
  • .length gets the number of characters in the string.
  • .length获取字符串中的字符数。

#3


9  

For me its not text box its a span tag and this worked for me.

对我来说,它不是文本框,它是一个span标签,这对我很有用。

var len = $("span").text().length;

#4


3  

Below mentioned code works perfectly fine for taking length of any characters entered in textbox.

下面的代码可以很好地处理文本框中输入的任何字符的长度。

$("#Texboxid").val().length;

$(" # Texboxid ").val . length();

#5


2  

CODE

$('#montant-total-prevu').on("change", function() {

var taille = $('#montant-total-prevu').val().length;

    if (taille > 9) {

//TODO

}

});

#6


0  

You need to only grab the element with an appropriate jQuery selector and then the .val() method to get the string contained in the input textbox and then call the .length on that string.

您只需使用适当的jQuery选择器获取元素,然后使用.val()方法获取输入文本框中包含的字符串,然后调用该字符串的.length。

$('input:text').val().length

$('输入:文本').val(). length

However, be warned that if the selector matches multiple inputs, .val() will only return the value of the first textbox. You can also change the selector to get a more specific element but keep the :text to ensure it's an input textbox.

但是,请注意,如果选择器匹配多个输入,.val()将只返回第一个文本框的值。您还可以更改选择器以获得更特定的元素,但保留:text以确保它是一个输入文本框。

On another note, to get the length of a string contained in another, non-input element, you can use the .text() function to get the string and then use .length on that string to find its length.

在另一个注释中,要获取另一个非输入元素中包含的字符串的长度,您可以使用.text()函数获取字符串,然后使用.length在该字符串上找到它的长度。