How can I get form data with JavaScript/jQuery?

前端 未结 28 2211
不知归路
不知归路 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:28

    $(form).serializeArray().reduce(function (obj, item) {
          if (obj[item.name]) {
               if ($.isArray(obj[item.name])) {
                   obj[item.name].push(item.value);
               } else {
                    var previousValue = obj[item.name];
                    obj[item.name] = [previousValue, item.value];
               }
          } else {
               obj[item.name] = item.value;
          }
    
         return obj;
    }, {});
    

    It will fix issue:couldn't work with multiselects.

提交回复
热议问题