jQuery 输入字段值的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16441275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:08:37  来源:igfitidea点击:

Input field value's length

jqueryhtmlinput

提问by eomeroff

Is there a way with jQuery to find out the width of text inserted in input field of type text? I need that info because I need to perform some work when certain with is achieved. Also it would be very helpful for me to find the jQuery event that is occurring when user is entering one more character in input field which will make the first character of the whole value invisible?

jQuery 有没有办法找出文本类型输入字段中插入的文本宽度?我需要这些信息,因为我需要在实现确定时执行一些工作。此外,当用户在输入字段中再输入一个字符时,找到发生的 jQuery 事件也会非常有帮助,这将使整个值的第一个字符不可见?

Please see example http://jsfiddle.net/J58ht/2/

请参见示例http://jsfiddle.net/J58ht/2/

<input style="width: 35px;" type="text"> <span></span>


    $('input').on('keyup',function(){
      var input = $(this);
      input.next("span").text(input.val().length + " chars");
});

Try entering characters 123456 in the input field. When entering char 6 the first char 1 will be invisible.

尝试在输入字段中输入字符 123456。输入字符 6 时,第一个字符 1 将不可见。

I need that event, when value overlaps input.

当值与输入重叠时,我需要该事件。

回答by Gautam3164

You can find the length of the value by using jQuery's val()method which returns the current value of a form element as a string. Then you can use the length propertyfrom that string.

您可以使用 jQuery 的val()方法找到值的长度,该方法将表单元素的当前值作为字符串返回。然后您可以使用该字符串的长度属性

$('input').on('keyup',function(){
      alert('This length is ' + $(this).val().length);
});

Here's a working example on jsFiddle: http://jsfiddle.net/J58ht/

这是一个关于 jsFiddle 的工作示例:http: //jsfiddle.net/J58ht/

based on your edited question it should be like

根据您编辑的问题,它应该是这样的

$('input').on('keyup',function(){
      var my_txt = $(this).val();
      var len = my_txt.length;
      if(len > my_constant_length)
      {
          var res = my_txt.substring(1,my_constant_length);
          $(this).val(res);
      }
});

回答by Freelancer

Try to use:

尝试使用:

var LengthOfText = $("#txtInputText").val().length;

回答by PSR

try this

尝试这个

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

.length

。长度

回答by Shijin TR

You can use length

您可以使用长度

    jQuery(selector).val().length;

Eg,

例如,

      $('#id').val().length;   
      $('#class name').val().length;   

回答by Santosh

Try this

尝试这个

var inputLen = $("#txtInputText")[0].length

回答by Suresh Atta

you can try

你可以试试

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

And remember lengthis not a function.

并且记住length不是一个函数。

回答by roger_that

var value = $("#textFieldId").val();
var fieldLength = value.length

This works.

这有效。

回答by VenkateshKumar

To find out the length of text you can use 

var len = $("#id").val().length;

$("#id").keyup(function(){
  var textLength = $(this).val().length;
  if(textLength > 15){
    alert('Length is exceeded');
    //Do ur stuff;
  }
});