How can I get form data with JavaScript/jQuery?

前端 未结 28 1922
不知归路
不知归路 2020-11-22 12:39

Is there a simple, one-line way to get the data of a form as it would be if it was to be submitted in the classic HTML-only way?

For example:



        
相关标签:
28条回答
  • 2020-11-22 13:17

    I wrote a library to solve this very problem: JSONForms. It takes a form, goes through each input and builds a JSON object you can easily read.

    Say you have the following form:

    <form enctype='application/json'>
      <input name='places[0][city]' value='New York City'>
      <input type='number' name='places[0][population]' value='8175133'>
      <input name='places[1][city]' value='Los Angeles'>
      <input type='number' name='places[1][population]' value='3792621'>
      <input name='places[2][city]' value='Chicago'>
      <input type='number' name='places[2][population]' value='2695598'>
    </form>
    

    Passing the form to JSONForms' encode method returns you the following object:

    {
      "places": [
        {
          "city": "New York City",
          "population": 8175133
        },
        {
          "city": "Los Angeles",
          "population": 3792621
        },
        {
          "city": "Chicago",
          "population": 2695598
        }
      ]
    }
    

    Here's demo with your form.

    0 讨论(0)
  • 2020-11-22 13:19

    This method should do it. It serializes the form data and then converts them to an object. Takes care of groups of checkboxes as well.

    function getFormObj(formId) {
      var formParams = {};
      $('#' + formId)
        .serializeArray()
        .forEach(function(item) {
          if (formParams[item.name]) {
            formParams[item.name] = [formParams[item.name]];
            formParams[item.name].push(item.value)
          } else {
            formParams[item.name] = item.value
          }
        });
      return formParams;
    }
    
    0 讨论(0)
  • 2020-11-22 13:20

    use .serializeArray() to get the data in array format and then convert it into an object:

    function getFormObj(formId) {
        var formObj = {};
        var inputs = $('#'+formId).serializeArray();
        $.each(inputs, function (i, input) {
            formObj[input.name] = input.value;
        });
        return formObj;
    }
    
    0 讨论(0)
  • 2020-11-22 13:21

    Updated answer for 2014: HTML5 FormData does this

    var formData = new FormData(document.querySelector('form'))
    

    You can then post formData exactly as it is - it contains all names and values used in the form.

    0 讨论(0)
  • 2020-11-22 13:22
    function getFormData($form){
        var unindexed_array = $form.serializeArray();
        var indexed_array = {};
    
        $.map(unindexed_array, function(n, i){
            if(indexed_array[n['name']] == undefined){
                indexed_array[n['name']] = [n['value']];
            }else{
                indexed_array[n['name']].push(n['value']);
            }
        });
    
        return indexed_array;
    }
    
    0 讨论(0)
  • 2020-11-22 13:22

    You are all not fully correct. You cannot write:

    formObj[input.name] = input.value;
    

    Because this way if you have multiselect list - its values will be overwritten with the last one, since it's transmitted as: "param1" : "value1", "param1" : "value2".

    So, correct approach is:

    if (formData[input.name] === undefined) {
        formData[input.name] = input.value;
    }
    else {
        var inputFieldArray = $.merge([], $.isArray(formData[input.name]) ? formData[input.name] : [formData[input.name]]);
        $.merge(inputFieldArray, [input.value]);
        formData[input.name] = $.merge([], inputFieldArray);
    }
    
    0 讨论(0)
提交回复
热议问题