jquery text area length count?

后端 未结 5 1123
别跟我提以往
别跟我提以往 2021-02-03 22:21


I have a text area field where i need to provide information about the word count when the user enters some text in the field. Length of the field is supposed to be 500 Ch

相关标签:
5条回答
  • 2021-02-03 22:25

    Simplest way to count:

    var count = $("#your_textarea").val().length;
    
    0 讨论(0)
  • 2021-02-03 22:39

    Try to use this plugin feature

    jquery-character-counter from jqeasy.com

    0 讨论(0)
  • 2021-02-03 22:41

    i use

    jQuery Textarea Characters Counter Plugin

    Examples and documentation at: http://roy-jin.appspot.com/jsp/textareaCounter.jsp

    0 讨论(0)
  • 2021-02-03 22:43

    I was having trouble with the onkeydown event and had success with onkeyup. Here's the code I came up with to count down the remaining characters (with a limit of 120)

    $(function() {
      var input = $('#my-input'), display = $('.char-count'), count = 0, limit = 120;
    
      count = input.val().length
      remaining = limit - count
      update(remaining);
    
      input.keyup(function(e) {
        count = $(this).val().length;
        remaining = limit - count;
    
        update(remaining);
      });
    
      function update(count) {
        var txt = ( Math.abs(count) === 1 ) ? count + ' Character Remaining' :  count + ' Characters Remaining'
        display.html(txt);
      }
    
    });
    
    0 讨论(0)
  • 2021-02-03 22:49
    $("#your-text-area").on('keyup', function(event) {
        var currentString = $("#your-text-area").val()
        $("Your Div").html(currentString.length);
        if (currentString.length <= 500 )  {  /*or whatever your number is*/
           //do some css with your div
        } else {
           //do some different stuff with your div
        }
    });
    

    http://api.jquery.com/bind/

    0 讨论(0)
提交回复
热议问题