跨浏览器的情况下,限制textarea输入框中文字的长度是一件不容易的事情。你需要至少考虑3点:
1.通过键盘输入字符;
2.通过paste输入(control+v 或 右键菜单);
3.通过拖拽输入文字;
在longboo中,可以用以下思路(通过同步和异步的处理)来达到动态限制文字输入的目的:- Class('App', 'linb.Com',{
- Instance:{
- autoDestroy:true,
- iniComponents:function(){
- // [[code created by jsLinb UI Builder
- var host=this, children=[], append=function(child){children.push(child.get(0))};
- append((new linb.UI.Input)
- .host(host,"input555")
- .setLeft(100)
- .setTop(80)
- .setHeight(100)
- .setMultiLines(true)
- .setDynCheck(true)
- .onChange("_input555_onChange")
- .beforeKeypress("_input555_bk")
- );
- return children;
- // ]]code created by jsLinb UI Builder
- },
- // 同步:防止用键盘直接敲入
- _input555_bk:function(profile, caret, key, control, shift, alt) {
- if(key.length==1){
- if(control && (key=='c'||key=='x'||key=='a'))
- return true;
- var len=profile.boxing().getUIValue().length - (caret[1]-caret[0]);
- if (len >= 10)
- return false;
- }
- },
- // 异步:防止用paste或drop的方式输入
- _input555_onChange:function (profile, oldValue, newValue) {
- if (newValue.length > 10)
- profile.boxing().setUIValue(newValue.substr(0,10),true);
- }
- }
- });
复制代码 |