form serialize javascript (no framework)

后端 未结 23 1634
一生所求
一生所求 2020-11-22 16:07

Wondering is there a function in javascript without jquery or any framework that allows me to serialize the form and access the serialized version?

23条回答
  •  情书的邮戳
    2020-11-22 16:47

    document.serializeForm = function (selector) {
         var dictionary = {};
         var form = document.querySelector(selector);
         var formdata = new FormData(form);
         var done = false;
         var iterator = formdata.entries();
         do {
             var prop = iterator.next();
             if (prop.done && !prop.value) {
                 done = true;
             }
             else {
                 dictionary[prop.value[0]] = prop.value[1];
             }
    
         } while (!done);
         return dictionary;
    }
    

提交回复
热议问题