Heim > Web-Frontend > js-Tutorial > TextArea 控件的最大长度问题(js json)_表单特效

TextArea 控件的最大长度问题(js json)_表单特效

WBOY
Freigeben: 2016-05-16 18:39:15
Original
1125 Leute haben es durchsucht

如果只是单纯地想限制 textarea 中的字数,不想写太多的话,可用:







在 textarea 的 onkeyup 中检查字数之方法:

复制代码 代码如下:




o 设定好自动去检查所有的 textarea 之方法:
复制代码 代码如下:







复制代码 代码如下:

var Inputs = {

OnIntegerOnly: function() {
var k = System.isIE ? event.keyCode : event.which;

if(event.ctrlKey || event.ctrlAlt || event.shiftKey) {
Inputs.DisableInput(event);
return;
}

if ( ( k 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& ( k 105 )
) {
Inputs.DisableInput(event);
}

return k;
},

OnDecimalOnly: function() {
var k = System.isIE ? event.keyCode : event.which;

if(event.ctrlKey || event.ctrlAlt || event.shiftKey) {
Inputs.DisableInput(event);
return;
}

if( ( k 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 190 != k
&& 110 != k
&& ( k 105 )
) {
Inputs.DisableInput(event);
}

return k;
},

OnAsciiOnly: function() {
var k = System.isIE ? event.keyCode : event.which;

if ( ( k 122 )
&& ( k 90 )
&& ( k 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
Inputs.DisableInput(event);
}

return k;
},

DisableInput: function(event) {
if(System.isIE)
event.returnValue = false;
else
event.preventDefault();
},

AddOnPos: function(obj, charvalue) {
//obj代表要插入字符的输入框

//value代表要插入的字符

obj.focus();
var r = document.selection.createRange();
var ctr = obj.createTextRange();
var i;
var s = obj.value;

//注释掉的这种方法只能用在单行的输入框input内

//对多行输入框textarea无效
//r.setEndPoint("StartToStart", ctr);
//i = r.text.length;
//取到光标位置----Start----
var ivalue = "&^asdjfls2FFFF325%$^&";
r.text = ivalue;
i = obj.value.indexOf(ivalue);
r.moveStart("character", -ivalue.length);
r.text = "";
//取到光标位置----End----
//插入字符
obj.value = s.substr(0,i) + charvalue + s.substr(i,s.length);
ctr.collapse(true);
ctr.moveStart("character", i + charvalue.length);
ctr.select();
},

MaxLength: function() {
var _this = event.srcElement;
var iMaxLen = parseInt(_this.getAttribute('maxlength'));

switch(event.type) {
case 'beforepaste': {
event.returnValue = false;
} break;

case 'paste': {
var iTxtLen = _this.value.lenUTF8();
var txtCbd = window.clipboardData.getData("Text");
var iCbdLen = txtCbd.lenUTF8();
if((iMaxLen - iTxtLen) var iSurplusLen = iMaxLen - iTxtLen;
var subStr = txtCbd.substr(0, iSurplusLen);
var insertStr = '';
for(var i = 0; i var chr = subStr.substr(i, 1);
if(iSurplusLen >= chr.lenUTF8()) {
insertStr += chr;
iSurplusLen -= chr.lenUTF8();
}
}

Inputs.AddOnPos(_this, insertStr);
//_this.style.backgroundColor = 'red';
//System.setTimeout(Inputs.ColorTips, 500, _this);
event.returnValue = false;
event.srcElement.style.imeMode = 'disabled';
} else {
event.returnValue = true;
event.srcElement.style.imeMode = 'auto';
}
} break;

case 'keypress': {
var iTxtLen = _this.value.lenUTF8();
if(iMaxLen while(_this.value.lenUTF8() > iMaxLen) {
_this.value = _this.value.substr(0, _this.value.length - 1);
}
Inputs.DisableInput(event);
if('disabled' != event.srcElement.style.imeMode) {
event.srcElement.style.imeMode = 'disabled';
}
}
else {
if('auto' != event.srcElement.style.imeMode) {
event.srcElement.style.imeMode = 'auto';
}
}
} break;

case 'keyup': {
var k = System.isIE ? event.keyCode : event.which;

if (8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
if(_this.getAttribute) {
_this.fireEvent('onkeypress');
}
}
} break;

case 'keydown': {
var k = System.isIE ? event.keyCode : event.which;

if (8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
var iTxtLen = _this.value.lenUTF8();
if(iMaxLen Inputs.DisableInput(event);
}
}
} break;
}
},

TextAreaMaxLength: function() {

var aX = document.getElementsByTagName('textarea');
for(var i = 0; i if(aX[i].getAttribute('maxlength')) {
aX[i].detachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].detachEvent('onpaste', Inputs.MaxLength);
aX[i].detachEvent('onkeypress', Inputs.MaxLength);
aX[i].detachEvent('onkeyup', Inputs.MaxLength);
aX[i].detachEvent('onkeydown', Inputs.MaxLength);

aX[i].attachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].attachEvent('onpaste', Inputs.MaxLength);
aX[i].attachEvent('onkeypress', Inputs.MaxLength);
aX[i].attachEvent('onkeyup', Inputs.MaxLength);
aX[i].attachEvent('onkeydown', Inputs.MaxLength);
/*
var res = 'zh-cn' == window.navigator.systemLanguage
? '最大长度:'
: 'Maximum length for: ';

if('' == aX[i].title) aX[i].title = res + aX[i].getAttribute('maxlength');
*/
}
}
},

TextBoxMaxLength: function() {
var aX = document.getElementsByTagName('input');
for(var i = 0; i var maxLen = aX[i].getAttribute('maxlength');
if('text' == aX[i].type
&& null != maxLen
&& '' != maxLen
&& undefined != maxLen
&& 2147483647 > parseInt(maxLen)
&& '' == aX[i].title) {

aX[i].detachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].detachEvent('onpaste', Inputs.MaxLength);
aX[i].detachEvent('onkeypress', Inputs.MaxLength);
aX[i].detachEvent('onkeyup', Inputs.MaxLength);

aX[i].attachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].attachEvent('onpaste', Inputs.MaxLength);
aX[i].attachEvent('onkeypress', Inputs.MaxLength);
aX[i].attachEvent('onkeyup', Inputs.MaxLength);
/*
var res = 'zh-cn' == window.navigator.systemLanguage
? '最大长度:'
: 'Maximum length for: ';

aX[i].title = res + aX[i].getAttribute('maxlength');
*/
}
}
}
}

Tools.Inputs = Inputs;

window.attachEvent('onload', Tools.Inputs.TextAreaMaxLength);
window.attachEvent('onload', Tools.Inputs.TextBoxMaxLength);
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage