Countdown available spaces in a textarea with jquery or other?

后端 未结 9 1166
無奈伤痛
無奈伤痛 2020-12-07 19:17

I have a few areas on my site where I need to limit text input to X amount of characters and it\'s nice to show a number of spaces left while a user types in, like twitter d

9条回答
  •  一生所求
    2020-12-07 19:53

    Using jQuery, assuming that you have a textarea with id field that you wish to limit to 100 characters, and another element with id chars-remaining to display the number of available characters:

    var max = 100;
    $('#field').keyup(function() {
        if($(this).val().length > max) {
            $(this).val($(this).val().substr(0, max));
        }
        $('#chars-remaining').html((max - $(this).val().length) + ' characters remaining');
    });
    

提交回复
热议问题