// get all form values into json
var s2=$('form').values();
// restore all forms values back
$('form').values(s2);
Here is the function
/* function for form saving restoring code */
$.fn.values = function (data) {
var els = $(this).find(':input').get();
if (typeof data != 'object') {
// return all data
data = {};
$.each(els, function () {
if (this.name && !this.disabled && this.type != "hidden") {
if (this.type == 'checkbox') {
data[this.name] = $(this).is(":checked");
} else if (this.type == 'radio') {
if ($(this).is(":checked")) {
data[this.name] = $(this).val();
}
} else {
data[this.name] = $(this).val();
}
}
});
return data;
} else {
$.each(els, function () {
if (this.name && data[this.name] != null) {
//console.log(">" + this.name + " " + data[this.name]);
if (this.type == 'checkbox') {
$(this).attr("checked", data[this.name]); //(data[this.name] == $(this).val())
//console.log("checkbox");
} else if (this.type == 'radio') {
$(this).attr("checked", data[this.name] == $(this).val());
} else {
$(this).val(data[this.name]);
}
}
});
return $(this);
}
};
No comments:
Post a Comment