限制 HTML5 textarea 中的行数 [英] Limit rows in HTML5 textarea

查看:48
本文介绍了限制 HTML5 textarea 中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找问题的解决方案,但找不到.它可以在 AngularJS 或 Javascript 中(然后我将在 AngularJS 中翻译它).问题是我必须限制一个简单的 textarea 的行.HTML5 的属性 'rows=x' 只限制视图.我必须限制线条.问题是,即使在图形上线条向下,组件也会将其视为一条独特的线条.用户必须按 ENTER 才能创建新行.但是,我必须限制行.我做了这个指令:

I'm looking for a solution to a problem, but I'm not able to find it. It could be in AngularJS or in Javascript (then I'll translate it in AngularJS). The problem is that I have to limit the rows of a simple textarea. The attribute 'rows=x' of HTML5 limit just the view. I have to limit the lines. The problem is that even if graphycally the lines go down, the components looks at it as a unique line. The user has to press ENTER to create a new line. But, I have to limit the lines. I did this directive:

angular.module('app').directive('maxlines', function (): any {
return function (scope: any, element: any, attrs: any) {
    element.bind("keydown keypress", function (event: any) {

        if (event.which === 13) {
            var text = element.val(),
                numberOfLines = (text.match(/\n/g) || []).length + 1,
                maxRows = parseInt(element.attr('rows'));

            if (event.which === 13 && numberOfLines === maxRows) {
                return false;
            }
        }
    });
};
});

如果我按 ENTER 可以工作,但如果我不按 Enter 继续写就不行.

It works if I press ENTER, but it doesnt work if I continue to write withouth press enter.

推荐答案

认为你可以用纯 HTML 做到这一点.

I think you can do this with pure HTML.