form serialize javascript (no framework)

后端 未结 23 1643
一生所求
一生所求 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 17:02

    Here is pure JavaScript approach:

    var form = document.querySelector('form');
    var data = new FormData(form);
    var req = new XMLHttpRequest();
    req.send(data);
    

    Though it seems to be working only for POST requests.

    https://developer.mozilla.org/en-US/docs/Web/API/FormData

提交回复
热议问题