jquery uses the serialize (), Serializearray () method to get form data + string and object type form submission methods

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/zqtsx/article/details/28655717

Original form form value acquisition method (manual):

[JavaScript]View PlainCopy
    1. $.ajax ({
    2. Type: "POST",
    3. URL: "ajax.php",
    4. Data: "name= the star &position=it technology",
    5. Success: function (msg) {alert (msg);},
    6. Error: function (Error) {alert (error);}
    7. });

The JQ serialize () method takes the value:

[JavaScript]View PlainCopy
    1. $.ajax ({
    2. Type: "POST",
    3. URL:"ajax.php",
    4. data:$ (' #formID '). Serialize (),//form to submit
    5. Success: function (msg) {alert (msg);},
    6. Error: function (Error) {alert (error);}
    7. });

Serialize () Serialization of form instances:

[HTML]View PlainCopy
  1. <script type="Text/javascript" src="Jquery-1.9.1.min.js"></script >
  2. <script type="Text/javascript">
  3. $ (function () {
  4. $ ("#button"). Click (function () {
  5. Alert ($ ("#formID"). Serialize ());
  6. });
  7. });
  8. </Script>
  9. <form id="FormID">
  10. Name <input value= "pick up theSky Star" name="name" />
  11. Jobs <input value="It Technology" name="position" />
  12. <input id="button" value="Submit" type="button" />
  13. </form>

converts a value in a form to a key-value pair:

[JavaScript]View PlainCopy
  1. For example: {Name: ' Extract Sky Stars ', Position: ' It technology '}
  2. PS: note to place the same name in an array
  3. function Getformjson (form) {
  4. var o = {};
  5. var a = $ (form). Serializearray ();
  6. $.each (A, function () {
  7. if (o[this.name]!== undefined) {
  8. if (!o[this.name].push) {
  9. o[THIS.name] = [o[this.name]];
  10. }
  11. o[this.name].push (this.value | |  ");
  12. } Else {
  13. o[THIS.name] = This.value | |  ";
  14. }
  15. });
  16. return o;
  17. }

Ajax calls for key-value pairs:

[JavaScript]View PlainCopy
  1. Debug Calls
  2. $ (function () {
  3. $ ("#button"). Click (function () {
  4. Alert (Getformjson ("#formID"));
  5. });
  6. });
  7. Ajax Submissions
  8. $.ajax ({
  9. Type: "POST",
  10. URL:"ajax.php",
  11. 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
  12. DataType: ' json ',
  13. Success: function (msg) {alert (msg);},
  14. Error: function (Error) {alert (error);}
  15. });

Common HTML forms in an instance:

[HTML]View PlainCopy
    1. <form id="FormID">
    2. Name <input value= "pick up theSky Star" name="name" />
    3. Jobs <input value="It Technology" name="position" />
    4. <input id="button" value="Submit" type="button" />
    5. </form>

jquery uses the serialize (), Serializearray () method to get form data + string and object type form submission methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.