1. $ ('textarea # txtPrizeNote '); // specifies the name of the textarea control.
2. The 'span 'tab displays the remaining words.
HTML:
Copy codeThe Code is as follows:
<Div>
<Textarea id = "txtPrizeNote" runat = "server" height = "74px" width = "480px" maxlength = "10"
Style = "width: 480px; height: 74px; float: left"> </textarea>
<Span style = "color: Red;"> * </span> <br/>
Remaining words: <span id = "showmsg" style = "color: red"> </span>
</Div>
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Return the byte length of val.
Function getByteLen (val ){
Var len = 0;
For (var I = 0; I <val. length; I ++ ){
If (val [I]. match (/[^ \ x00-\ xff]/ig )! = Null) // fullwidth
Len + = 2;
Else
Len + = 1;
}
Return len;
}
// Return the value of val in the specified byte length max.
Function getByteVal (val, max ){
Var returnValue = '';
Var byteValLen = 0;
For (var I = 0; I <val. length; I ++ ){
If (val [I]. match (/[^ \ x00-\ xff]/ig )! = Null)
ByteValLen + = 2;
Else
ByteValLen + = 1;
If (byteValLen> max)
Break;
ReturnValue + = val [I];
}
Return returnValue;
}
$ (Function (){
Var _ area = $ ('textarea # txtPrizeNote ');
Var _ info = _ area. next ();
Var _ max = _ area. attr ('maxlength ');
Var _ val;
_ Area. bind ('keyup change', function () {// bind the keyup and change events
If (_ info. find ('span '). size () <1) {// avoid inserting a prompt message every time a bullet occurs.
_ Info. append (_ max );
}
_ Val = $ (this). val ();
_ Cur = getByteLen (_ val );
If (_ cur = 0) {// when the default value is 0, you can enter the default value maxlength.
_ Info. text (_ max );
} Else if (_ cur <_ max) {// when the default value is smaller than the limit, the number of inputs can be max-cur.
_ Info. text (_ max-_ cur );
} Else {// when the default value is greater than or equal to the limit
_ Info. text (0 );
$ (This). val (getByteVal (_ val, _ max); // intercept the value within the specified byte length
}
});
});
</Script>