Jquery. form. js usage-Method for clearing form _ jquery

Source: Internet
Author: User
This article mainly introduces jquery. form. js to clear the form method. If you need it, you can refer to the following code to extract it from jquery. form. in js, this method is highly useful and can be used independently.
This section of code is concise and can be used as a reference for learning.

The Code is as follows:


/**
* Clears the form data. Takes the following actions on the form's input fields:
*-Input text fields will have their 'value' property set to the empty string
*-Select elements will have their 'selectedindex 'property set to-1
*-Checkbox and radio inputs will have their 'checked' property set to false
*-Inputs of type submit, button, reset, and hidden will ** not * be converted ted
*-Button elements will ** not * be specified ted
*/
$. Fn. clearForm = function (includeHidden ){
Return this. each (function (){
$ ('Input, select, textarea ', this). clearFields (includeHidden); // this indicates setting the context. When multiple forms exist, only the called form is used.
});
};

$. Fn. clearFields = $. fn. clearInputs = function (includeHidden ){
Var re =/^ (? : Color | date | datetime | email | month | number | password | range | search | tel | text | time | url | week) $/I; // 'did' is not in this list
Return this. each (function (){
Var t = this. type, tag = this. tagName. toLowerCase ();
If (re. test (t) | tag = 'textarea '){
This. value = '';
}
Else if (t = 'checkbox' | t = 'Radio '){
This. checked = false;
}
Else if (tag = 'select '){
This. selectedIndex =-1;
}
Else if (t = "file "){
If (/MSIE/. test (navigator. userAgent )){
$ (This). replaceWith ($ (this). clone (true ));
} Else {
$ (This). val ('');
}
}
Else if (includeHidden ){
// IncludeHidden can be the value true, or it can be a selector string
// Indicating a special test; for example:
// $ ('# Myform'). clearForm ('. special: Den den ')
// The above wocould clean hidden inputs that have the class of 'special'
If (includeHidden === true &/hidden/. test (t) |
(Typeof includeHidden = 'string' & $ (this). is (includeHidden ))){
This. value = '';
}
}
});
};

Related Article

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.