Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. (function($) {
  2. $.extend($.fn, {
  3. serializeJson: function() {
  4. var payload = {},
  5. regex = /^(\w+)\[(\d*)\]$/;
  6.  
  7. $.each($(this).serializeArray(), function(i, input) {
  8. var res = input.name.match(regex);
  9. if(res) {
  10. if(! (res[1] in payload)) {
  11. payload[res[1]] = [];
  12. }
  13.  
  14. var array = payload[res[1]];
  15.  
  16. if(res[2] == '') {
  17. array[array.length] = input.value;
  18. } else {
  19. // Ensure that the value gets inserted at exactly
  20. // the right spot.
  21. while(array.length < res[2]) {
  22. array[array.length] = null;
  23. }
  24.  
  25. array.splice(res[2], 0, input.value);
  26. }
  27. } else {
  28. payload[input.name] = input.value;
  29. }
  30. });
  31.  
  32. return JSON.stringify(payload);
  33. }
  34. });
  35. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement