Original form value acquisition method (manual ):
$. Ajax ({type: "POST", url: "ajax. php ", data:" Name = extract star & position = IT technology ", success: function (msg) {alert (msg) ;}, error: function (error) {alert (error );}});
JQ serialize () method value:
$. Ajax ({type: "POST", url: "ajax. php ", data: $ ('# formID '). serialize (), // form to be submitted success: function (msg) {alert (msg) ;}, error: function (error) {alert (error );}}); serialize () serialized form instance: <type = "text/" src = "jquery-1.9.1.min.js"> </> <type = "text/"> $ (function () {$ ("# button "). click (function () {alert ($ ("# formID "). serialize ());});}); </> <form id = "formID"> name <input value = "" Name = "name"/> position <input value = "IT technology" name = "position" /> <input id = "button" value = "Submit" type = "button"/> </form>
Convert the value in form to a key-Value Pair:
// For example: {Name: 'extract stars from the start', position: 'It techno'} // ps: Put the same Name in an array. function getFormJson (form) {var o ={}; var a =$ (form ). serializeArray (); $. each (a, function () {if (o [this. name]! = Undefined) {if (! O [this. name]. push) {o [this. name] = [o [this. name];} o [this. name]. push (this. value | '');} else {o [this. name] = this. value | '';}}); return o ;}
Key-Value Pair AJAX call:
// Debug call $ (function () {$ ("# button "). click (function () {alert (getFormJson ("# formID") ;}); // Ajax submit $. ajax ({type: "POST", url: "ajax. php ", data: getFormJson ($ (" # formID "), // fill in the Form ID or the form dataType to be submitted in the form data JSON format function parameter: 'json', success: function (msg) {alert (msg) ;}, error: function (error) {alert (error );}});
Common HTML forms for instances:
<Form id = "formID"> name <input value = "" Name = "name"/> position <input value = "IT technology" name = "position"/> <input id = "button" value = "Submit" type = "button"/> </form>