Html 文本区域最大长度不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3491598/
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-29 04:09:06  来源:igfitidea点击:

Text Area maxlength not working

html

提问by Sachin R

I want to set maximum length to the textarea. I am using following code for the same,but it is not working,

我想为 textarea 设置最大长度。我正在使用以下代码,但它不起作用,

<textarea name="txtDescription" cols=10 rows=3 maxlength=50></textarea>

But it is not working,it takes characters beyond 50.

但它不起作用,它需要超过 50 个字符。

采纳答案by Darin Dimitrov

There's no maxlengthattribute defined for textarea. You need to implementthis using javascript.

没有maxlength为 定义属性textarea。您需要使用 javascript实现这一点。

回答by JoenasE

Yup maxlength does not work in textarea. But you can use jQuery something like this:

是的 maxlength 在 textarea 中不起作用。但是你可以像这样使用jQuery:

var $limitNum = 500;
$('textarea[name="textarea_name"]').keydown(function() {
    var $this = $(this);

    if ($this.val().length > $limitNum) {
        $this.val($this.val().substring(0, $limitNum));
    }
});

回答by Dinch

Answers above are correct for earlier version of IE 10.

上面的答案对于 IE 10 的早期版本是正确的。

If you are having problem with maxlength attribure, my recommendation is check your browser version first. The maxlength attribute is new for the tag in HTML5. It should be just fine on IE 10+ or Chrome.

如果您对 maxlength 属性有疑问,我的建议是先检查您的浏览器版本。maxlength 属性是 HTML5 中标签的新属性。在 IE 10+ 或 Chrome 上应该没问题。

回答by Luiggi ZAMOL

I tried maxlength in textarea, but does not count the characters correctly(also counts the EOLs). I use max_lengthattribute instead of normal maxlength.

我在 textarea 中尝试了 maxlength,但没有正确计算字符(也计算 EOL)。我使用max_lengthattribute 而不是 normal maxlength

HTML:

HTML:

<textarea name="whatever" max_length="100"></textarea>

JS:

JS:

$('textarea').keyup(function(){
  var maxlength = parseInt($(this).attr('max_length')),
      text = $(this).val(),
      eol = text.match(/(\r\n|\n|\r)/g),
      count_eol = $.isArray(eol) ? eol.length : 0,//error if eol is null
      count_chars = text.length - count_eol;
  if (maxlength && count_chars > maxlength)
    $(this).val(text.substring(0, maxlength + count_eol));
});

回答by Douwe de Haan

When the answer of Darin Dimitrov was posted back in 2010, the maxlengthattribute wasn't implemented apparently.

当 Darin Dimitrov 的答案在 2010 年发布时,该maxlength属性显然没有实现。

Right know, in 2018, it is.

没错,在 2018 年,确实如此。

Sources

来源

Example

例子

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <label for="without">Without maxlength</label>
  <textarea id="without" class="form-control"></textarea>
  <br>
  <label for="with">With maxlength="10"</label>
  <textarea id="with" maxlength="10" class="form-control"></textarea>
</div>

回答by Frank Nwoko

Maxlength attribute for textarea works in Chrome.

textarea 的 Maxlength 属性适用于 Chrome。

回答by Tassadaq Hussain

you can use

您可以使用

<textarea name="testString" onkeypress="if (this.value.length >= 250) { return false; }"id="" cols="30" rows="10" maxlength="250"></textarea>

回答by FarrahH

This works for Ruby on Rails :

这适用于 Ruby on Rails:

:maxlength => "10"

code example:

代码示例:

<%= text_area 'order', 'special_instructions_display', :size => "85x7", :maxlength => "10"  %>

回答by Lion

You need to put it into the HTML tag like this, make sure you have airquotes "".

你需要像这样把它放到 HTML 标签中,确保你有空气引号“”。

<textarea maxlength="5"></textarea>

回答by user335160

please use jquery for reference

请使用jquery作为参考

<script type="text/javascript">  
$().ready(function(){   

     $("#textareaId").attr('maxlength','20');   

 });
</script>