function Htmlformelement () {this.init (); return This.json;} HTMLFormElement.prototype.init = function () {This.json = {};this.inputs = Document.queryselectorall ("input"); This.texts = Document.queryselectorall ("textarea"); this.sels = Document.queryselectorall ("select"); this.types = [' CheckBox ', ' Color ', ' Date ', ' datetime ', ' datetime-local ', ' Month ', ' Week ', ' time ', ' email ', ' file ', ' hidden ', ' number ', ' p Assword ', ' Radio ', ' range ', ' search ', ' tel ', ' text ', ' url '];this. Serializedjson ();}; HTMLFormElement.prototype.SerializedJson = function () {if (this.inputs.length > 0 | | this.texts.length > 0) {THIS.G Etinputsvalue (); This.gettextvalue (); This.getselsvalue ();}}; HTMLFormElement.prototype.getInputsValue = function () {var input;for (var i = 0; i < this.inputs.length; i++) {input = T His.inputs[i];var name = Input.getattribute ("name"), var type = Input.getattribute ("type"); if (Type && name & & This.types.indexOf (Type.tolowercase ()) >-1) {if (Type! = ' checkbox ' && type) ! = ' Radio ') {this.json[name] = Input.value;} else if (type = = ' Radio ') {if (!this.json[name]) {this.json[name] = ';} if (input.checked) {this.json[name] = Input.value;}} else if (type = = ' checkbox ') {if (!this.json[name]) {this.json[name] = ';} if (input.checked) {if (This.json[name]) {This.json[name] + = "," + Input.value} else {this.json[name] = Input.value;}}}} }htmlformelement.prototype.gettextvalue = function () {for (var i = 0; i < this.texts.length; i++) {input = This.texts[i] var name = Input.getattribute ("name"), if (name) {This.json[name] = Input.value;}}; }; HTMLFormElement.prototype.getSelsValue = function () {for (var i = 0; i < this.sels.length; i++) {input = This.sels[i];va R name = Input.getattribute ("name"), if (name) {This.json[name] = Input.value;}} return This.json; }
How to use:
New Htmlformelement ());
Htmlformelement get all the values in the form and return them in JSON form