Limit number of lines in textarea and Display line count using jQuery

蹲街弑〆低调 提交于 2019-11-26 03:56:59

问题


Using jQuery I would like to:

  • Limit the number of lines a user can enter in a textarea to a set number
  • Have a line counter appear that updates number of lines as lines are entered
  • Return key or /n would count as line

Kudos to anyone who can help!

$(document).ready(function(){
  $(\'#countMe\').keydown(function(event) {
    // If number of lines is > X (specified by me) return false
    // Count number of lines/update as user enters them turn red if over limit.

  });   
});


<form class=\"lineCount\">
  <textarea id=\"countMe\" cols=\"30\" rows=\"5\"></textarea><br>
  <input type=\"submit\" value=\"Test Me\">
</form>

<div class=\"theCount\">Lines used = X (updates as lines entered)<div>

For this example lets say limit the number of lines allowed to 10.

Thanks all!


回答1:


html:

<textarea id="countMe" cols="30" rows="5"></textarea>
<div class="theCount">Lines used: <span id="linesUsed">0</span><div>

js:

$(document).ready(function(){

    var lines = 10;
    var linesUsed = $('#linesUsed');

    $('#countMe').keydown(function(e) {

        newLines = $(this).val().split("\n").length;
        linesUsed.text(newLines);

        if(e.keyCode == 13 && newLines >= lines) {
            linesUsed.css('color', 'red');
            return false;
        }
        else {
            linesUsed.css('color', '');
        }
    });
});

fiddle: http://jsfiddle.net/XNCkH/17/




回答2:


Here is little improved code. In previous example you could paste text with more lines that you want.

HTML

<textarea data-max="10"></textarea>
<div class="theCount">Lines used: <span id="linesUsed">0</span></div>

JS

jQuery('document').on('keyup change', 'textarea', function(e){

        var maxLines = jQuery(this).attr('data-max');        
        newLines = $(this).val().split("\n").length;

        console.log($(this).val().split("\n"));

        if(newLines >= maxLines) {
            lines = $(this).val().split("\n").slice(0, maxLines);
            var newValue = lines.join("\n");
            $(this).val(newValue);
            $("#linesUsed").html(newLines);
            return false;
        }

    });



回答3:


A much ugly , but somehow working example specify rows of textarea

<textarea rows="3"></textarea>

and then in js

   $("textarea").on('keydown keypress keyup',function(e){
       if(e.keyCode == 8 || e.keyCode == 46){
           return true;
       }
       var maxRowCount = $(this).attr("rows") || 2;
        var lineCount = $(this).val().split('\n').length;
        if(e.keyCode == 13){
            if(lineCount == maxRowCount){
                return false;
            }
        }
        var jsElement = $(this)[0];
        if(jsElement.clientHeight < jsElement.scrollHeight){
            var text = $(this).val();
            text= text.slice(0, -1);
            $(this).val(text);
            return false;
        }

    });


来源:https://stackoverflow.com/questions/6501043/limit-number-of-lines-in-textarea-and-display-line-count-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!