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 = '';
}
}
});
};