Reprinted from: http://blog.csdn.net/zqtsx/article/details/28655717
Original form form value acquisition method (manual):
[JavaScript]View PlainCopy
- $.ajax ({
- Type: "POST",
- URL: "ajax.php",
- Data: "name= the star &position=it technology",
- Success: function (msg) {alert (msg);},
- Error: function (Error) {alert (error);}
- });
The JQ serialize () method takes the value:
[JavaScript]View PlainCopy
- $.ajax ({
- Type: "POST",
- URL:"ajax.php",
- data:$ (' #formID '). Serialize (),//form to submit
- Success: function (msg) {alert (msg);},
- Error: function (Error) {alert (error);}
- });
Serialize () Serialization of form instances:
[HTML]View PlainCopy
- <script type="Text/javascript" src="Jquery-1.9.1.min.js"></script >
- <script type="Text/javascript">
- $ (function () {
- $ ("#button"). Click (function () {
- Alert ($ ("#formID"). Serialize ());
- });
- });
- </Script>
- <form id="FormID">
- Name <input value= "pick up theSky Star" name="name" />
- Jobs <input value="It Technology" name="position" />
- <input id="button" value="Submit" type="button" />
- </form>
converts a value in a form to a key-value pair:
[JavaScript]View PlainCopy
- For example: {Name: ' Extract Sky Stars ', Position: ' It technology '}
- PS: note to place 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;
- }
Ajax calls for key-value pairs:
[JavaScript]View PlainCopy
- Debug Calls
- $ (function () {
- $ ("#button"). Click (function () {
- Alert (Getformjson ("#formID"));
- });
- });
- Ajax Submissions
- $.ajax ({
- Type: "POST",
- URL:"ajax.php",
- Data:getformjson ($ ("#formID")), theID of the form or the form to be submitted is filled out in the function parameter of the form data JSON format
- DataType: ' json ',
- Success: function (msg) {alert (msg);},
- Error: function (Error) {alert (error);}
- });
Common HTML forms in an instance:
[HTML]View PlainCopy
- <form id="FormID">
- Name <input value= "pick up theSky Star" name="name" />
- Jobs <input value="It Technology" name="position" />
- <input id="button" value="Submit" type="button" />
- </form>
jquery uses the serialize (), Serializearray () method to get form data + string and object type form submission methods