我希望能够限制textarea中的字符数.我使用的方法在谷歌浏览器中效果很好,但在Firefox中很慢,并且在IE中不起作用.
使用Javascript:
function len(){
t_v=textarea.value;
if(t_v.length>180){
long_post_container.innerHTML=long_post;
post_button.className=post_button.className.replace('post_it_regular','post_it_disabled');
post_button.disabled=true;
}
else{
long_post_container.innerHTML="";
post_button.className=post_button.className.replace('post_it_disabled','post_it_regular');
post_button.disabled=false;
}
if(t_v.length>186){
t_v=t_v.substring(0,186);
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<textarea id="user_post_textarea" name="user_post_textarea" cols="28" rows="1" onkeypress="len();" onkeyup="len();"></textarea>
Run Code Online (Sandbox Code Playgroud)
body元素底部的Javascript:
textarea=document.getElementById('user_post_textarea');
Run Code Online (Sandbox Code Playgroud) 我试图限制在textarea中输入的字符数,如下所示:
<div>
@Html.TextAreaFor(x => x.StudentName, new { @maxlength = "1000" })
</div>
Run Code Online (Sandbox Code Playgroud)
这在Firefox和Chrome中运行良好,但maxlength属性在IE中不起作用.我怎么做??
是否可以检测到粘贴到HTML文本区域中的字符数,如果超出限制则取消粘贴?
编辑:我想要做的是阻止用户粘贴大量的字符(约300万),因为它崩溃了一些浏览器.所以我想在浏览器锁定之前取消粘贴.我正在制作一个文档编辑器,用户可能会尝试这样做.但他们可以根据自己的意愿打字.