Home > Web Front-end > JS Tutorial > jquery.form.js usage method to clear form_jquery

jquery.form.js usage method to clear form_jquery

WBOY
Release: 2016-05-16 16:56:17
Original
1367 people have browsed it

This code is excerpted from jquery.form.js. Because I think this method is very useful, it can also be used independently.
This code snippet is concise and can be used as a good reference for learning.

Copy code 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 effected
 * - button elements will *not* be effected
 */
$ .fn.clearForm = function(includeHidden) {
return this.each(function() {
$('input,select,textarea', this).clearFields(includeHidden); //this means setting the context , when there are multiple forms, only the called form will be affected
});
};

$.fn.clearFields = $.fn.clearInputs = function(includeHidden) {
var re = /^(?:color|date|datetime|email|month|number|password|range|search|tel |text|time|url|week)$/i; // 'hidden' is not in this list
return this.each(function() {
var t = this.type, tag = this.tagName .toLowerCase();
                                                                                                                                                                                                      checkbox' || t == 'radio') {
this.checked = false;
}
else if (tag == 'select') {
this.selectedIndex = -1;
} Else if (t == "file") {
if (/msie/.testck.com)) {
$ (this) .Replacewith ($ (this) .clone (true));
                                                                                                                               // includeHidden can be the value true, or it can be a selector string
                                                                                                                                                                                                                     / the above would clean hidden inputs that have the class of 'special'
if ( (includeHidden === true && /hidden/.test(t)) ||
(typeof includeHidden == 'string' && $(this).is(includeHidden)) ) {
                   this.value = '';

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template