TEXTAREA does not support maxlength solutions (jquery) _jquery

Source: Internet
Author: User
With. NET control for a long time, in a page to increase the control length of the textbox, simply add a maxlength= ' xxx ' on the line, but the test is always the reason is set a multiline mode, in which case the generated HTML code is textarea, At the same time, the MaxLength attribute is not added because IE does not support textarea's maxlength attribute, so the test in Firefox 6 shows that Firefox supports this attribute. So it's easy. Write yourself a jquery extension so it's easy to implement the maximum length for textarea control.
The extension code is as follows:
Copy Code code 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 = Window.document.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);

Above only for IE control copy and paste control and input control, because of the use of IE characteristics, these methods in Firefox is ineffective.
Calling code:
Copy Code code as follows:

$ (document). Ready (function () {
$ ("#ctl00_ZiiOContent_ Ucquestionnaire_txtquestion4_2 "). Textarealimit ();
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.