jquery serialized form as JSON object

Source: Internet
Author: User


[HTML]
View PlainCopy
  1. <form id="MyForm">
  2. <table>
  3. <tr>
  4. <TD> Name:</td>
  5. <TD> <input type="text" name="name" /> </TD >
  6. </tr>
  7. <tr>
  8. <TD> Gender:</td>
  9. <TD>
  10. <input type="Radio" name="Sex" value="1"> Male
  11. <input type="Radio" name="Sex" value="0"> Female
  12. </td>
  13. </tr>
  14. <tr>
  15. <TD> Age:</td>
  16. <TD>
  17. <Select name="age">
  18. <option value=>20</option>
  19. <option value=>21</option>
  20. <option value=>22</option>
  21. </Select>
  22. </td>
  23. </tr>
  24. <tr>
  25. <TD> Hobby </td>
  26. <TD>
  27. <input type="checkbox" value="basketball" name="hobby"> Basketball
  28. <input type="checkbox" value="Volleyball" name="hobby"> Volleyball
  29. <input type="checkbox" value="Football" name="hobby"> Soccer
  30. <input type="checkbox" value="Earth" name="hobby"> Earth
  31. </td>
  32. </tr>
  33. <tr>
  34. <TD Colspan="2">
  35. <input type="button" id= "ajaxbtn" value="commit" />
  36. </td>
  37. </tr>
  38. </Table>
  39. </form>
[JavaScript]View PlainCopy
  1. <script type="Text/javascript" >
  2. $ (function () {
  3. $ ("#ajaxBtn"). Click (function () {
  4. var params = $ ("#myform"). SerializeObject (); //Serializing a form as a JSON object
  5. Console.info (params);
  6. })
  7. })
  8. $.fn.serializeobject = function () {
  9. var o = {};
  10. var a = This.serializearray ();
  11. $.each (A, function () {
  12. if (o[this.name]) {
  13. if (!o[this.name].push) {
  14. o[THIS.name] = [o[this.name]];
  15. }
  16. o[this.name].push (this.value | |  ");
  17. } Else {
  18. o[THIS.name] = This.value | |  ";
  19. }
  20. });
  21. return o;
  22. }
  23. </script>

The SerializeObject method above is to serialize the form into a JSON object

jquery serialized form as JSON object

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.