form serialize javascript (no framework)

后端 未结 23 1549
一生所求
一生所求 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:03

    If you are looking to serialize the inputs on an event. Here's a pure JavaScript approach I use.

    // serialize form
    var data = {};
    var inputs = [].slice.call(e.target.getElementsByTagName('input'));
    inputs.forEach(input => {
      data[input.name] = input.value;
    });
    

    Data will be a JavaScript object of the inputs.

提交回复
热议问题