Use. net Control for a long time, when adding control length control to the TextBox of a page, simply add a maxlength = 'xxx', but the test is always not good, the reason is that the multiline mode is set. In this case, the generated html code is textarea, And the maxlength attribute is not added because IE does not support the maxlength attribute of textarea. Therefore, in firefox 6, we found that firefox supports this attribute. That's easy. Write a jquery extension, so that you can easily control the maximum length of textarea.
The extension code is as follows:
Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. textarealimit = function (settings ){
Settings = jQuery. extend ({
Length: 1000
}, Settings );
MaxLength = settings. length;
$ (This ). attr ("maxlength", maxLength ). bind ("keydown", doKeydown ). bind ("keypress", doKeypress ). bind ("beforepaste", doBeforePaste ). bind ("paste", doPaste );
Function doKeypress ()
{
Var oTR = document. selection. createRange ()
If (oTR. text. length> = 1)
Event. returnValue = true
Else if (this. value. length> maxLength-1)
Event. returnValue = false
}
Function doKeydown ()
{
Var _ obj = this;
SetTimeout (function ()
{
If (_ obj. value. length> maxLength-1)
{
Var oTR = too many Doc ument. selection. createRange ()
OTR. moveStart ("character",-1 * (_ obj. value. length-maxLength ))
OTR. text = ""
}
}, 1)
}
Function doBeforePaste ()
{
Event. returnValue = false
}
Function doPaste ()
{
Event. returnValue = false
Var oTR = document. selection. createRange ()
Var iInsertLength = maxLength-this. value. length + oTR. text. length
Var sData = window. clipboardData. getData ("Text"). substr (0, iInsertLength)
OTR. text = sData;
}
}
}) (JQuery );
The above is only for IE to control the copy and paste control and the input control, due to the use of IE features, these methods are invalid in firefox.
Call code:
Copy codeThe Code is as follows:
$ (Document). ready (function (){
$ ("# Ctl00_ZiiOContent_ucQuestionnaire_txtquestion4_2"). textarealimit ();
});