多行文本框长度 [英] Multiline TextBox Length
本文介绍了多行文本框长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有多行文本框,使用javascript可以进行最大长度验证,但是可以正常工作,但是通过鼠标单击事件复制粘贴数据,它不能正常工作,我已经使用了所有mouseevents和onchange事件,该事件仅适用于下一个鼠标操作,但是我当文本框获得inputvalue时想要脚本调用对不起我的英语
任何建议
i have multiline textbox with maximum length validation using javascript its work fine but copy paste the data through mouse click event its not work properly already i use all mouseevents and onchange event this events only work for the next mouse action but i want the script call when the textbox get the inputvalue sorry for my english
any suggestion
推荐答案
The onchange should do the job. try this sample.
<html>
<script type="text/javascript">
function LimitSize(s){
if(mytxt.value.length>s)
mytxt.value = mytxt.value.substring(0,s);
}
</script>
<textarea id="mytxt" rows="2" cols="20" onchange="LimitSize(10);">
abc
</textarea>
</html>
Kethu的答案很接近,但是您可能还希望在其中添加其他处理程序.
还要添加一个onpaste="LimitSize(10);
处理程序.
Kethu''s answer is close, but you may wish to add a different handler in as well.
Add aonpaste="LimitSize(10);
handler as well.
function LimitSize( maxLength )
{
var clipboardValue = window.clipboardData.getData('Text');
var ctl = document.getElementById('mytxt');
if( clipboardValue.length > maxLength )
{
ctl.value = clipboardValue.substring(0,maxLength);
}
else
{
ctl.value = clipboardValue;
}
}
这篇关于多行文本框长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文