Advertisement
stuppid_bot

serialize form

Mar 6th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function serialize(form) {
  2.     if (!(form instanceof HTMLFormElement)) {
  3.         return;
  4.     }
  5.     var q = [];
  6.     function push(k, v) {
  7.         q.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
  8.     }
  9.     for (var i = 0, el, j; i < form.elements.length; ++i) {
  10.         el = form.elements[i];
  11.         if (!el.name || (el.tagName == 'INPUT' && (el.type == 'file' || ((el.type == 'radio' || el.type == 'checkbox') && !el.checked)))) {
  12.             continue;
  13.         }
  14.         if (el.tagName == 'SELECT') {
  15.             if (el.multiple) {
  16.                for (j = 0; j < el.options.length; ++j) {
  17.                     if (el.options[j].selected) {
  18.                         push(el.name, el.options[j].value);
  19.                     }
  20.                 }
  21.             }
  22.             else {
  23.                 push(el.name, el.options[el.selectedIndex].value);
  24.             }
  25.         }
  26.         else {
  27.             push(el.name, el.value);
  28.         }
  29.     }
  30.     return q.join('&');
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement