Html TextArea 最大长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5116564/
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
TextArea Maximum Length?
提问by Histack
Is it possible to set the maximum length of text in a TextArea
?
是否可以在 a 中设置文本的最大长度TextArea
?
采纳答案by Peter Antonius
If you are using jQuery, use this plugin http://www.stjerneman.com/demo/maxlength-with-jquery
如果您使用 jQuery,请使用此插件 http://www.stjerneman.com/demo/maxlength-with-jquery
回答by Etdashou
Something interesting is that HTML5 have added the feature of maxlength to textarea, if HTML5 is something you are ok to use right now.
有趣的是,如果 HTML5 是您现在可以使用的东西,那么 HTML5 已将 maxlength 的功能添加到 textarea 中。
Demo:
演示:
<textarea maxlength="20"></textarea>
回答by ruveena
You can use following format in HTML
您可以在 HTML 中使用以下格式
<input type="text" maxlength="13">
回答by Hali
This solution is re-usable for all text areas via one function and it doesn't inform the user that he/she is typing too many characters, it prevents them from doing so, sort of like maxlength
此解决方案可通过一个功能对所有文本区域重复使用,并且不会通知用户他/她正在输入太多字符,而是阻止他们这样做,有点像 maxlength
The Function:
功能:
<script language="javascript" type="text/javascript">
function imposeMaxLength(Object, MaxLen)
{
return (Object.value.length <= MaxLen);
}
</script>
Implementation:
执行:
<textarea name="myName" onkeypress="return imposeMaxLength(this, 15);" ><textarea>
回答by Victor
The Textarea doesn't accept the maxlength.
Textarea 不接受最大长度。
I have created a javascript function to handle this on onchange event. On one of my solutions I avoid the submit on form onsubmit event.
我创建了一个 javascript 函数来处理 onchange 事件。在我的解决方案之一中,我避免了提交表单 onsubmit 事件。
The code bellow will avoid submit if the textarea has more than 255 caracters
如果 textarea 超过 255 个字符,下面的代码将避免提交
<script>
function checkSize(){
var x = document.getElementById('x');
return x.value.length <= 255;
}
</script>
<form onsubmit="return checkSize()">
<textarea id="x"><textarea>
</form>